• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  GPBWriteRawVarint32(&state_, value);
378}
379
380- (void)writeEnum:(int32_t)fieldNumber value:(int32_t)value {
381  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
382  GPBWriteRawVarint32(&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
456- (void)writeDoubleArray:(int32_t)fieldNumber
457                  values:(GPBDoubleArray *)values
458                     tag:(uint32_t)tag {
459  if (tag != 0) {
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);
465    }];
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];
471    }];
472  } else {
473    [values enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
474#pragma unused(idx, stop)
475      [self writeDouble:fieldNumber value:value];
476    }];
477  }
478}
479
480//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Float, Float, float, )
481// This block of code is generated, do not edit it directly.
482
483- (void)writeFloatArray:(int32_t)fieldNumber
484                 values:(GPBFloatArray *)values
485                    tag:(uint32_t)tag {
486  if (tag != 0) {
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);
492    }];
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];
498    }];
499  } else {
500    [values enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
501#pragma unused(idx, stop)
502      [self writeFloat:fieldNumber value:value];
503    }];
504  }
505}
506
507//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(UInt64, UInt64, uint64_t, )
508// This block of code is generated, do not edit it directly.
509
510- (void)writeUInt64Array:(int32_t)fieldNumber
511                  values:(GPBUInt64Array *)values
512                     tag:(uint32_t)tag {
513  if (tag != 0) {
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);
519    }];
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];
525    }];
526  } else {
527    [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
528#pragma unused(idx, stop)
529      [self writeUInt64:fieldNumber value:value];
530    }];
531  }
532}
533
534//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Int64, Int64, int64_t, )
535// This block of code is generated, do not edit it directly.
536
537- (void)writeInt64Array:(int32_t)fieldNumber
538                 values:(GPBInt64Array *)values
539                    tag:(uint32_t)tag {
540  if (tag != 0) {
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);
546    }];
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];
552    }];
553  } else {
554    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
555#pragma unused(idx, stop)
556      [self writeInt64:fieldNumber value:value];
557    }];
558  }
559}
560
561//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Int32, Int32, int32_t, )
562// This block of code is generated, do not edit it directly.
563
564- (void)writeInt32Array:(int32_t)fieldNumber
565                 values:(GPBInt32Array *)values
566                    tag:(uint32_t)tag {
567  if (tag != 0) {
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);
573    }];
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];
579    }];
580  } else {
581    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
582#pragma unused(idx, stop)
583      [self writeInt32:fieldNumber value:value];
584    }];
585  }
586}
587
588//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(UInt32, UInt32, uint32_t, )
589// This block of code is generated, do not edit it directly.
590
591- (void)writeUInt32Array:(int32_t)fieldNumber
592                  values:(GPBUInt32Array *)values
593                     tag:(uint32_t)tag {
594  if (tag != 0) {
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);
600    }];
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];
606    }];
607  } else {
608    [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
609#pragma unused(idx, stop)
610      [self writeUInt32:fieldNumber value:value];
611    }];
612  }
613}
614
615//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Fixed64, UInt64, uint64_t, )
616// This block of code is generated, do not edit it directly.
617
618- (void)writeFixed64Array:(int32_t)fieldNumber
619                   values:(GPBUInt64Array *)values
620                      tag:(uint32_t)tag {
621  if (tag != 0) {
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);
627    }];
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];
633    }];
634  } else {
635    [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
636#pragma unused(idx, stop)
637      [self writeFixed64:fieldNumber value:value];
638    }];
639  }
640}
641
642//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Fixed32, UInt32, uint32_t, )
643// This block of code is generated, do not edit it directly.
644
645- (void)writeFixed32Array:(int32_t)fieldNumber
646                   values:(GPBUInt32Array *)values
647                      tag:(uint32_t)tag {
648  if (tag != 0) {
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);
654    }];
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];
660    }];
661  } else {
662    [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
663#pragma unused(idx, stop)
664      [self writeFixed32:fieldNumber value:value];
665    }];
666  }
667}
668
669//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SInt32, Int32, int32_t, )
670// This block of code is generated, do not edit it directly.
671
672- (void)writeSInt32Array:(int32_t)fieldNumber
673                  values:(GPBInt32Array *)values
674                     tag:(uint32_t)tag {
675  if (tag != 0) {
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);
681    }];
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];
687    }];
688  } else {
689    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
690#pragma unused(idx, stop)
691      [self writeSInt32:fieldNumber value:value];
692    }];
693  }
694}
695
696//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SInt64, Int64, int64_t, )
697// This block of code is generated, do not edit it directly.
698
699- (void)writeSInt64Array:(int32_t)fieldNumber
700                  values:(GPBInt64Array *)values
701                     tag:(uint32_t)tag {
702  if (tag != 0) {
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);
708    }];
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];
714    }];
715  } else {
716    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
717#pragma unused(idx, stop)
718      [self writeSInt64:fieldNumber value:value];
719    }];
720  }
721}
722
723//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SFixed64, Int64, int64_t, )
724// This block of code is generated, do not edit it directly.
725
726- (void)writeSFixed64Array:(int32_t)fieldNumber
727                    values:(GPBInt64Array *)values
728                       tag:(uint32_t)tag {
729  if (tag != 0) {
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);
735    }];
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];
741    }];
742  } else {
743    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
744#pragma unused(idx, stop)
745      [self writeSFixed64:fieldNumber value:value];
746    }];
747  }
748}
749
750//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SFixed32, Int32, int32_t, )
751// This block of code is generated, do not edit it directly.
752
753- (void)writeSFixed32Array:(int32_t)fieldNumber
754                    values:(GPBInt32Array *)values
755                       tag:(uint32_t)tag {
756  if (tag != 0) {
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);
762    }];
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];
768    }];
769  } else {
770    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
771#pragma unused(idx, stop)
772      [self writeSFixed32:fieldNumber value:value];
773    }];
774  }
775}
776
777//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Bool, Bool, BOOL, )
778// This block of code is generated, do not edit it directly.
779
780- (void)writeBoolArray:(int32_t)fieldNumber
781                values:(GPBBoolArray *)values
782                   tag:(uint32_t)tag {
783  if (tag != 0) {
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);
789    }];
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];
795    }];
796  } else {
797    [values enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
798#pragma unused(idx, stop)
799      [self writeBool:fieldNumber value:value];
800    }];
801  }
802}
803
804//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Enum, Enum, int32_t, Raw)
805// This block of code is generated, do not edit it directly.
806
807- (void)writeEnumArray:(int32_t)fieldNumber
808                values:(GPBEnumArray *)values
809                   tag:(uint32_t)tag {
810  if (tag != 0) {
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);
816    }];
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];
822    }];
823  } else {
824    [values enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
825#pragma unused(idx, stop)
826      [self writeEnum:fieldNumber value:value];
827    }];
828  }
829}
830
831//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(String, NSString)
832// This block of code is generated, do not edit it directly.
833
834- (void)writeStringArray:(int32_t)fieldNumber values:(NSArray *)values {
835  for (NSString *value in values) {
836    [self writeString:fieldNumber value:value];
837  }
838}
839
840//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Message, GPBMessage)
841// This block of code is generated, do not edit it directly.
842
843- (void)writeMessageArray:(int32_t)fieldNumber values:(NSArray *)values {
844  for (GPBMessage *value in values) {
845    [self writeMessage:fieldNumber value:value];
846  }
847}
848
849//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Bytes, NSData)
850// This block of code is generated, do not edit it directly.
851
852- (void)writeBytesArray:(int32_t)fieldNumber values:(NSArray *)values {
853  for (NSData *value in values) {
854    [self writeBytes:fieldNumber value:value];
855  }
856}
857
858//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Group, GPBMessage)
859// This block of code is generated, do not edit it directly.
860
861- (void)writeGroupArray:(int32_t)fieldNumber values:(NSArray *)values {
862  for (GPBMessage *value in values) {
863    [self writeGroup:fieldNumber value:value];
864  }
865}
866
867//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(UnknownGroup, GPBUnknownFieldSet)
868// This block of code is generated, do not edit it directly.
869
870- (void)writeUnknownGroupArray:(int32_t)fieldNumber values:(NSArray *)values {
871  for (GPBUnknownFieldSet *value in values) {
872    [self writeUnknownGroup:fieldNumber value:value];
873  }
874}
875
876//%PDDM-EXPAND-END (19 expansions)
877
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);
886}
887
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);
895}
896
897- (void)flush {
898  if (state_.output != nil) {
899    GPBRefreshBuffer(&state_);
900  }
901}
902
903- (void)writeRawByte:(uint8_t)value {
904  GPBWriteRawByte(&state_, value);
905}
906
907- (void)writeRawData:(const NSData *)data {
908  [self writeRawPtr:[data bytes] offset:0 length:[data length]];
909}
910
911- (void)writeRawPtr:(const void *)value
912             offset:(size_t)offset
913             length:(size_t)length {
914  if (value == nil || length == 0) {
915    return;
916  }
917
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;
924  } else {
925    // Write extends past current buffer.  Fill the rest of this buffer and
926    // flush.
927    size_t bytesWritten = bufferBytesLeft;
928    memcpy(state_.bytes + state_.position, ((uint8_t *)value) + offset,
929           bytesWritten);
930    offset += bytesWritten;
931    length -= bytesWritten;
932    state_.position = bufferLength;
933    GPBRefreshBuffer(&state_);
934    bufferLength = state_.size;
935
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;
943    } else {
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:@""];
948      }
949    }
950  }
951}
952
953- (void)writeTag:(uint32_t)fieldNumber format:(GPBWireFormat)format {
954  GPBWriteTagWithFormat(&state_, fieldNumber, format);
955}
956
957- (void)writeRawVarint32:(int32_t)value {
958  GPBWriteRawVarint32(&state_, value);
959}
960
961- (void)writeRawVarintSizeTAs32:(size_t)value {
962  // Note the truncation.
963  GPBWriteRawVarint32(&state_, (int32_t)value);
964}
965
966- (void)writeRawVarint64:(int64_t)value {
967  GPBWriteRawVarint64(&state_, value);
968}
969
970- (void)writeRawLittleEndian32:(int32_t)value {
971  GPBWriteRawLittleEndian32(&state_, value);
972}
973
974- (void)writeRawLittleEndian64:(int64_t)value {
975  GPBWriteRawLittleEndian64(&state_, value);
976}
977
978#pragma clang diagnostic pop
979
980@end
981
982size_t GPBComputeDoubleSizeNoTag(Float64 value) {
983#pragma unused(value)
984  return LITTLE_ENDIAN_64_SIZE;
985}
986
987size_t GPBComputeFloatSizeNoTag(Float32 value) {
988#pragma unused(value)
989  return LITTLE_ENDIAN_32_SIZE;
990}
991
992size_t GPBComputeUInt64SizeNoTag(uint64_t value) {
993  return GPBComputeRawVarint64Size(value);
994}
995
996size_t GPBComputeInt64SizeNoTag(int64_t value) {
997  return GPBComputeRawVarint64Size(value);
998}
999
1000size_t GPBComputeInt32SizeNoTag(int32_t value) {
1001  if (value >= 0) {
1002    return GPBComputeRawVarint32Size(value);
1003  } else {
1004    // Must sign-extend.
1005    return 10;
1006  }
1007}
1008
1009size_t GPBComputeSizeTSizeAsInt32NoTag(size_t value) {
1010  return GPBComputeInt32SizeNoTag((int32_t)value);
1011}
1012
1013size_t GPBComputeFixed64SizeNoTag(uint64_t value) {
1014#pragma unused(value)
1015  return LITTLE_ENDIAN_64_SIZE;
1016}
1017
1018size_t GPBComputeFixed32SizeNoTag(uint32_t value) {
1019#pragma unused(value)
1020  return LITTLE_ENDIAN_32_SIZE;
1021}
1022
1023size_t GPBComputeBoolSizeNoTag(BOOL value) {
1024#pragma unused(value)
1025  return 1;
1026}
1027
1028size_t GPBComputeStringSizeNoTag(NSString *value) {
1029  NSUInteger length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
1030  return GPBComputeRawVarint32SizeForInteger(length) + length;
1031}
1032
1033size_t GPBComputeGroupSizeNoTag(GPBMessage *value) {
1034  return [value serializedSize];
1035}
1036
1037size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value) {
1038  return value.serializedSize;
1039}
1040
1041size_t GPBComputeMessageSizeNoTag(GPBMessage *value) {
1042  size_t size = [value serializedSize];
1043  return GPBComputeRawVarint32SizeForInteger(size) + size;
1044}
1045
1046size_t GPBComputeBytesSizeNoTag(NSData *value) {
1047  NSUInteger valueLength = [value length];
1048  return GPBComputeRawVarint32SizeForInteger(valueLength) + valueLength;
1049}
1050
1051size_t GPBComputeUInt32SizeNoTag(int32_t value) {
1052  return GPBComputeRawVarint32Size(value);
1053}
1054
1055size_t GPBComputeEnumSizeNoTag(int32_t value) {
1056  return GPBComputeRawVarint32Size(value);
1057}
1058
1059size_t GPBComputeSFixed32SizeNoTag(int32_t value) {
1060#pragma unused(value)
1061  return LITTLE_ENDIAN_32_SIZE;
1062}
1063
1064size_t GPBComputeSFixed64SizeNoTag(int64_t value) {
1065#pragma unused(value)
1066  return LITTLE_ENDIAN_64_SIZE;
1067}
1068
1069size_t GPBComputeSInt32SizeNoTag(int32_t value) {
1070  return GPBComputeRawVarint32Size(GPBEncodeZigZag32(value));
1071}
1072
1073size_t GPBComputeSInt64SizeNoTag(int64_t value) {
1074  return GPBComputeRawVarint64Size(GPBEncodeZigZag64(value));
1075}
1076
1077size_t GPBComputeDoubleSize(int32_t fieldNumber, double value) {
1078  return GPBComputeTagSize(fieldNumber) + GPBComputeDoubleSizeNoTag(value);
1079}
1080
1081size_t GPBComputeFloatSize(int32_t fieldNumber, float value) {
1082  return GPBComputeTagSize(fieldNumber) + GPBComputeFloatSizeNoTag(value);
1083}
1084
1085size_t GPBComputeUInt64Size(int32_t fieldNumber, uint64_t value) {
1086  return GPBComputeTagSize(fieldNumber) + GPBComputeUInt64SizeNoTag(value);
1087}
1088
1089size_t GPBComputeInt64Size(int32_t fieldNumber, int64_t value) {
1090  return GPBComputeTagSize(fieldNumber) + GPBComputeInt64SizeNoTag(value);
1091}
1092
1093size_t GPBComputeInt32Size(int32_t fieldNumber, int32_t value) {
1094  return GPBComputeTagSize(fieldNumber) + GPBComputeInt32SizeNoTag(value);
1095}
1096
1097size_t GPBComputeFixed64Size(int32_t fieldNumber, uint64_t value) {
1098  return GPBComputeTagSize(fieldNumber) + GPBComputeFixed64SizeNoTag(value);
1099}
1100
1101size_t GPBComputeFixed32Size(int32_t fieldNumber, uint32_t value) {
1102  return GPBComputeTagSize(fieldNumber) + GPBComputeFixed32SizeNoTag(value);
1103}
1104
1105size_t GPBComputeBoolSize(int32_t fieldNumber, BOOL value) {
1106  return GPBComputeTagSize(fieldNumber) + GPBComputeBoolSizeNoTag(value);
1107}
1108
1109size_t GPBComputeStringSize(int32_t fieldNumber, NSString *value) {
1110  return GPBComputeTagSize(fieldNumber) + GPBComputeStringSizeNoTag(value);
1111}
1112
1113size_t GPBComputeGroupSize(int32_t fieldNumber, GPBMessage *value) {
1114  return GPBComputeTagSize(fieldNumber) * 2 + GPBComputeGroupSizeNoTag(value);
1115}
1116
1117size_t GPBComputeUnknownGroupSize(int32_t fieldNumber,
1118                                  GPBUnknownFieldSet *value) {
1119  return GPBComputeTagSize(fieldNumber) * 2 +
1120         GPBComputeUnknownGroupSizeNoTag(value);
1121}
1122
1123size_t GPBComputeMessageSize(int32_t fieldNumber, GPBMessage *value) {
1124  return GPBComputeTagSize(fieldNumber) + GPBComputeMessageSizeNoTag(value);
1125}
1126
1127size_t GPBComputeBytesSize(int32_t fieldNumber, NSData *value) {
1128  return GPBComputeTagSize(fieldNumber) + GPBComputeBytesSizeNoTag(value);
1129}
1130
1131size_t GPBComputeUInt32Size(int32_t fieldNumber, uint32_t value) {
1132  return GPBComputeTagSize(fieldNumber) + GPBComputeUInt32SizeNoTag(value);
1133}
1134
1135size_t GPBComputeEnumSize(int32_t fieldNumber, int32_t value) {
1136  return GPBComputeTagSize(fieldNumber) + GPBComputeEnumSizeNoTag(value);
1137}
1138
1139size_t GPBComputeSFixed32Size(int32_t fieldNumber, int32_t value) {
1140  return GPBComputeTagSize(fieldNumber) + GPBComputeSFixed32SizeNoTag(value);
1141}
1142
1143size_t GPBComputeSFixed64Size(int32_t fieldNumber, int64_t value) {
1144  return GPBComputeTagSize(fieldNumber) + GPBComputeSFixed64SizeNoTag(value);
1145}
1146
1147size_t GPBComputeSInt32Size(int32_t fieldNumber, int32_t value) {
1148  return GPBComputeTagSize(fieldNumber) + GPBComputeSInt32SizeNoTag(value);
1149}
1150
1151size_t GPBComputeSInt64Size(int32_t fieldNumber, int64_t value) {
1152  return GPBComputeTagSize(fieldNumber) +
1153         GPBComputeRawVarint64Size(GPBEncodeZigZag64(value));
1154}
1155
1156size_t GPBComputeMessageSetExtensionSize(int32_t fieldNumber,
1157                                         GPBMessage *value) {
1158  return GPBComputeTagSize(GPBWireFormatMessageSetItem) * 2 +
1159         GPBComputeUInt32Size(GPBWireFormatMessageSetTypeId, fieldNumber) +
1160         GPBComputeMessageSize(GPBWireFormatMessageSetMessage, value);
1161}
1162
1163size_t GPBComputeRawMessageSetExtensionSize(int32_t fieldNumber,
1164                                            NSData *value) {
1165  return GPBComputeTagSize(GPBWireFormatMessageSetItem) * 2 +
1166         GPBComputeUInt32Size(GPBWireFormatMessageSetTypeId, fieldNumber) +
1167         GPBComputeBytesSize(GPBWireFormatMessageSetMessage, value);
1168}
1169
1170size_t GPBComputeTagSize(int32_t fieldNumber) {
1171  return GPBComputeRawVarint32Size(
1172      GPBWireFormatMakeTag(fieldNumber, GPBWireFormatVarint));
1173}
1174
1175size_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.
1179    return result * 2;
1180  } else {
1181    return result;
1182  }
1183}
1184
1185size_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;
1191  return 5;
1192}
1193
1194size_t GPBComputeRawVarint32SizeForInteger(NSInteger value) {
1195  // Note the truncation.
1196  return GPBComputeRawVarint32Size((int32_t)value);
1197}
1198
1199size_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;
1209  return 10;
1210}
1211