aboutsummaryrefslogtreecommitdiffhomepage
path: root/objectivec/GPBCodedOutputStream.m
blob: 7c3ab44715c40397eb3cdb4bc7efec9a55181d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#import "GPBCodedOutputStream_PackagePrivate.h"

#import <mach/vm_param.h>

#import "GPBArray.h"
#import "GPBUnknownFieldSet_PackagePrivate.h"
#import "GPBUtilities_PackagePrivate.h"

// Structure for containing state of a GPBCodedInputStream. Brought out into
// a struct so that we can inline several common functions instead of dealing
// with overhead of ObjC dispatch.
typedef struct GPBOutputBufferState {
  uint8_t *bytes;
  size_t size;
  size_t position;
  NSOutputStream *output;
} GPBOutputBufferState;

@implementation GPBCodedOutputStream {
  GPBOutputBufferState state_;
  NSMutableData *buffer_;
}

static const int32_t LITTLE_ENDIAN_32_SIZE = sizeof(uint32_t);
static const int32_t LITTLE_ENDIAN_64_SIZE = sizeof(uint64_t);

// Internal helper that writes the current buffer to the output. The
// buffer position is reset to its initial value when this returns.
static void GPBRefreshBuffer(GPBOutputBufferState *state) {
  if (state->output == nil) {
    // We're writing to a single buffer.
    [NSException raise:@"OutOfSpace" format:@""];
  }
  if (state->position != 0) {
    NSInteger written =
        [state->output write:state->bytes maxLength:state->position];
    if (written != (NSInteger)state->position) {
      [NSException raise:@"WriteFailed" format:@""];
    }
    state->position = 0;
  }
}

static void GPBWriteRawByte(GPBOutputBufferState *state, uint8_t value) {
  if (state->position == state->size) {
    GPBRefreshBuffer(state);
  }
  state->bytes[state->position++] = value;
}

static void GPBWriteRawVarint32(GPBOutputBufferState *state, int32_t value) {
  while (YES) {
    if ((value & ~0x7F) == 0) {
      uint8_t val = (uint8_t)value;
      GPBWriteRawByte(state, val);
      return;
    } else {
      GPBWriteRawByte(state, (value & 0x7F) | 0x80);
      value = GPBLogicalRightShift32(value, 7);
    }
  }
}

static void GPBWriteRawVarint64(GPBOutputBufferState *state, int64_t value) {
  while (YES) {
    if ((value & ~0x7FL) == 0) {
      uint8_t val = (uint8_t)value;
      GPBWriteRawByte(state, val);
      return;
    } else {
      GPBWriteRawByte(state, ((int32_t)value & 0x7F) | 0x80);
      value = GPBLogicalRightShift64(value, 7);
    }
  }
}

static void GPBWriteInt32NoTag(GPBOutputBufferState *state, int32_t value) {
  if (value >= 0) {
    GPBWriteRawVarint32(state, value);
  } else {
    // Must sign-extend
    GPBWriteRawVarint64(state, value);
  }
}

static void GPBWriteUInt32(GPBOutputBufferState *state, int32_t fieldNumber,
                           uint32_t value) {
  GPBWriteTagWithFormat(state, fieldNumber, GPBWireFormatVarint);
  GPBWriteRawVarint32(state, value);
}

static void GPBWriteTagWithFormat(GPBOutputBufferState *state,
                                  uint32_t fieldNumber, GPBWireFormat format) {
  GPBWriteRawVarint32(state, GPBWireFormatMakeTag(fieldNumber, format));
}

static void GPBWriteRawLittleEndian32(GPBOutputBufferState *state,
                                      int32_t value) {
  GPBWriteRawByte(state, (value)&0xFF);
  GPBWriteRawByte(state, (value >> 8) & 0xFF);
  GPBWriteRawByte(state, (value >> 16) & 0xFF);
  GPBWriteRawByte(state, (value >> 24) & 0xFF);
}

static void GPBWriteRawLittleEndian64(GPBOutputBufferState *state,
                                      int64_t value) {
  GPBWriteRawByte(state, (int32_t)(value)&0xFF);
  GPBWriteRawByte(state, (int32_t)(value >> 8) & 0xFF);
  GPBWriteRawByte(state, (int32_t)(value >> 16) & 0xFF);
  GPBWriteRawByte(state, (int32_t)(value >> 24) & 0xFF);
  GPBWriteRawByte(state, (int32_t)(value >> 32) & 0xFF);
  GPBWriteRawByte(state, (int32_t)(value >> 40) & 0xFF);
  GPBWriteRawByte(state, (int32_t)(value >> 48) & 0xFF);
  GPBWriteRawByte(state, (int32_t)(value >> 56) & 0xFF);
}

- (void)dealloc {
  [self flush];
  [state_.output close];
  [state_.output release];
  [buffer_ release];

  [super dealloc];
}

- (instancetype)initWithOutputStream:(NSOutputStream *)output {
  NSMutableData *data = [NSMutableData dataWithLength:PAGE_SIZE];
  return [self initWithOutputStream:output data:data];
}

- (instancetype)initWithData:(NSMutableData *)data {
  return [self initWithOutputStream:nil data:data];
}

// This initializer isn't exposed, but it is the designated initializer.
// Setting OutputStream and NSData is to control the buffering behavior/size
// of the work, but that is more obvious via the bufferSize: version.
- (instancetype)initWithOutputStream:(NSOutputStream *)output
                                data:(NSMutableData *)data {
  if ((self = [super init])) {
    buffer_ = [data retain];
    [output open];
    state_.bytes = [data mutableBytes];
    state_.size = [data length];
    state_.output = [output retain];
  }
  return self;
}

+ (instancetype)streamWithOutputStream:(NSOutputStream *)output {
  NSMutableData *data = [NSMutableData dataWithLength:PAGE_SIZE];
  return [[[self alloc] initWithOutputStream:output
                                        data:data] autorelease];
}

+ (instancetype)streamWithData:(NSMutableData *)data {
  return [[[self alloc] initWithData:data] autorelease];
}

// Direct access is use for speed, to avoid even internally declaring things
// read/write, etc. The warning is enabled in the project to ensure code calling
// protos can turn on -Wdirect-ivar-access without issues.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdirect-ivar-access"

- (void)writeDoubleNoTag:(double)value {
  GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value));
}

- (void)writeDouble:(int32_t)fieldNumber value:(double)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64);
  GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value));
}

- (void)writeFloatNoTag:(float)value {
  GPBWriteRawLittleEndian32(&state_, GPBConvertFloatToInt32(value));
}

- (void)writeFloat:(int32_t)fieldNumber value:(float)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed32);
  GPBWriteRawLittleEndian32(&state_, GPBConvertFloatToInt32(value));
}

- (void)writeUInt64NoTag:(uint64_t)value {
  GPBWriteRawVarint64(&state_, value);
}

- (void)writeUInt64:(int32_t)fieldNumber value:(uint64_t)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  GPBWriteRawVarint64(&state_, value);
}

- (void)writeInt64NoTag:(int64_t)value {
  GPBWriteRawVarint64(&state_, value);
}

- (void)writeInt64:(int32_t)fieldNumber value:(int64_t)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  GPBWriteRawVarint64(&state_, value);
}

- (void)writeInt32NoTag:(int32_t)value {
  GPBWriteInt32NoTag(&state_, value);
}

- (void)writeInt32:(int32_t)fieldNumber value:(int32_t)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  GPBWriteInt32NoTag(&state_, value);
}

- (void)writeFixed64NoTag:(uint64_t)value {
  GPBWriteRawLittleEndian64(&state_, value);
}

- (void)writeFixed64:(int32_t)fieldNumber value:(uint64_t)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64);
  GPBWriteRawLittleEndian64(&state_, value);
}

- (void)writeFixed32NoTag:(uint32_t)value {
  GPBWriteRawLittleEndian32(&state_, value);
}

- (void)writeFixed32:(int32_t)fieldNumber value:(uint32_t)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed32);
  GPBWriteRawLittleEndian32(&state_, value);
}

- (void)writeBoolNoTag:(BOOL)value {
  GPBWriteRawByte(&state_, (value ? 1 : 0));
}

- (void)writeBool:(int32_t)fieldNumber value:(BOOL)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  GPBWriteRawByte(&state_, (value ? 1 : 0));
}

- (void)writeStringNoTag:(const NSString *)value {
  size_t length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  GPBWriteRawVarint32(&state_, (int32_t)length);
  if (length == 0) {
    return;
  }

  const char *quickString =
      CFStringGetCStringPtr((CFStringRef)value, kCFStringEncodingUTF8);

  // Fast path: Most strings are short, if the buffer already has space,
  // add to it directly.
  NSUInteger bufferBytesLeft = state_.size - state_.position;
  if (bufferBytesLeft >= length) {
    NSUInteger usedBufferLength = 0;
    BOOL result;
    if (quickString != NULL) {
      memcpy(state_.bytes + state_.position, quickString, length);
      usedBufferLength = length;
      result = YES;
    } else {
      result = [value getBytes:state_.bytes + state_.position
                     maxLength:bufferBytesLeft
                    usedLength:&usedBufferLength
                      encoding:NSUTF8StringEncoding
                       options:(NSStringEncodingConversionOptions)0
                         range:NSMakeRange(0, [value length])
                remainingRange:NULL];
    }
    if (result) {
      NSAssert2((usedBufferLength == length),
                @"Our UTF8 calc was wrong? %tu vs %zd", usedBufferLength,
                length);
      state_.position += usedBufferLength;
      return;
    }
  } else if (quickString != NULL) {
    [self writeRawPtr:quickString offset:0 length:length];
  } else {
    // Slow path: just get it as data and write it out.
    NSData *utf8Data = [value dataUsingEncoding:NSUTF8StringEncoding];
    NSAssert2(([utf8Data length] == length),
              @"Strings UTF8 length was wrong? %tu vs %zd", [utf8Data length],
              length);
    [self writeRawData:utf8Data];
  }
}

- (void)writeString:(int32_t)fieldNumber value:(NSString *)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatLengthDelimited);
  [self writeStringNoTag:value];
}

- (void)writeGroupNoTag:(int32_t)fieldNumber value:(GPBMessage *)value {
  [value writeToCodedOutputStream:self];
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatEndGroup);
}

- (void)writeGroup:(int32_t)fieldNumber value:(GPBMessage *)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatStartGroup);
  [self writeGroupNoTag:fieldNumber value:value];
}

- (void)writeUnknownGroupNoTag:(int32_t)fieldNumber
                         value:(const GPBUnknownFieldSet *)value {
  [value writeToCodedOutputStream:self];
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatEndGroup);
}

- (void)writeUnknownGroup:(int32_t)fieldNumber
                    value:(GPBUnknownFieldSet *)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatStartGroup);
  [self writeUnknownGroupNoTag:fieldNumber value:value];
}

- (void)writeMessageNoTag:(GPBMessage *)value {
  GPBWriteRawVarint32(&state_, (int32_t)[value serializedSize]);
  [value writeToCodedOutputStream:self];
}

- (void)writeMessage:(int32_t)fieldNumber value:(GPBMessage *)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatLengthDelimited);
  [self writeMessageNoTag:value];
}

- (void)writeBytesNoTag:(NSData *)value {
  GPBWriteRawVarint32(&state_, (int32_t)[value length]);
  [self writeRawData:value];
}

- (void)writeBytes:(int32_t)fieldNumber value:(NSData *)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatLengthDelimited);
  [self writeBytesNoTag:value];
}

- (void)writeUInt32NoTag:(uint32_t)value {
  GPBWriteRawVarint32(&state_, value);
}

- (void)writeUInt32:(int32_t)fieldNumber value:(uint32_t)value {
  GPBWriteUInt32(&state_, fieldNumber, value);
}

- (void)writeEnumNoTag:(int32_t)value {
  GPBWriteRawVarint32(&state_, value);
}

- (void)writeEnum:(int32_t)fieldNumber value:(int32_t)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  GPBWriteRawVarint32(&state_, value);
}

- (void)writeSFixed32NoTag:(int32_t)value {
  GPBWriteRawLittleEndian32(&state_, value);
}

- (void)writeSFixed32:(int32_t)fieldNumber value:(int32_t)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed32);
  GPBWriteRawLittleEndian32(&state_, value);
}

- (void)writeSFixed64NoTag:(int64_t)value {
  GPBWriteRawLittleEndian64(&state_, value);
}

- (void)writeSFixed64:(int32_t)fieldNumber value:(int64_t)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64);
  GPBWriteRawLittleEndian64(&state_, value);
}

- (void)writeSInt32NoTag:(int32_t)value {
  GPBWriteRawVarint32(&state_, GPBEncodeZigZag32(value));
}

- (void)writeSInt32:(int32_t)fieldNumber value:(int32_t)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  GPBWriteRawVarint32(&state_, GPBEncodeZigZag32(value));
}

- (void)writeSInt64NoTag:(int64_t)value {
  GPBWriteRawVarint64(&state_, GPBEncodeZigZag64(value));
}

- (void)writeSInt64:(int32_t)fieldNumber value:(int64_t)value {
  GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  GPBWriteRawVarint64(&state_, GPBEncodeZigZag64(value));
}

//%PDDM-DEFINE WRITE_PACKABLE_DEFNS(NAME, ARRAY_TYPE, TYPE, ACCESSOR_NAME)
//%- (void)write##NAME##Array:(int32_t)fieldNumber
//%       NAME$S     values:(GPB##ARRAY_TYPE##Array *)values
//%       NAME$S        tag:(uint32_t)tag {
//%  if (tag != 0) {
//%    if (values.count == 0) return;
//%    __block size_t dataSize = 0;
//%    [values enumerate##ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) {
//%#pragma unused(idx, stop)
//%      dataSize += GPBCompute##NAME##SizeNoTag(value);
//%    }];
//%    GPBWriteRawVarint32(&state_, tag);
//%    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
//%    [values enumerate##ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) {
//%#pragma unused(idx, stop)
//%      [self write##NAME##NoTag:value];
//%    }];
//%  } else {
//%    [values enumerate##ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) {
//%#pragma unused(idx, stop)
//%      [self write##NAME:fieldNumber value:value];
//%    }];
//%  }
//%}
//%
//%PDDM-DEFINE WRITE_UNPACKABLE_DEFNS(NAME, TYPE)
//%- (void)write##NAME##Array:(int32_t)fieldNumber values:(NSArray *)values {
//%  for (TYPE *value in values) {
//%    [self write##NAME:fieldNumber value:value];
//%  }
//%}
//%
//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Double, Double, double, )
// This block of code is generated, do not edit it directly.

- (void)writeDoubleArray:(int32_t)fieldNumber
                  values:(GPBDoubleArray *)values
                     tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeDoubleSizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeDoubleNoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeDouble:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Float, Float, float, )
// This block of code is generated, do not edit it directly.

- (void)writeFloatArray:(int32_t)fieldNumber
                 values:(GPBFloatArray *)values
                    tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeFloatSizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeFloatNoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeFloat:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(UInt64, UInt64, uint64_t, )
// This block of code is generated, do not edit it directly.

- (void)writeUInt64Array:(int32_t)fieldNumber
                  values:(GPBUInt64Array *)values
                     tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeUInt64SizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeUInt64NoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeUInt64:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Int64, Int64, int64_t, )
// This block of code is generated, do not edit it directly.

- (void)writeInt64Array:(int32_t)fieldNumber
                 values:(GPBInt64Array *)values
                    tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeInt64SizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeInt64NoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeInt64:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Int32, Int32, int32_t, )
// This block of code is generated, do not edit it directly.

- (void)writeInt32Array:(int32_t)fieldNumber
                 values:(GPBInt32Array *)values
                    tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeInt32SizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeInt32NoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeInt32:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(UInt32, UInt32, uint32_t, )
// This block of code is generated, do not edit it directly.

- (void)writeUInt32Array:(int32_t)fieldNumber
                  values:(GPBUInt32Array *)values
                     tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeUInt32SizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeUInt32NoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeUInt32:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Fixed64, UInt64, uint64_t, )
// This block of code is generated, do not edit it directly.

- (void)writeFixed64Array:(int32_t)fieldNumber
                   values:(GPBUInt64Array *)values
                      tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeFixed64SizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeFixed64NoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeFixed64:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Fixed32, UInt32, uint32_t, )
// This block of code is generated, do not edit it directly.

- (void)writeFixed32Array:(int32_t)fieldNumber
                   values:(GPBUInt32Array *)values
                      tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeFixed32SizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeFixed32NoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeFixed32:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SInt32, Int32, int32_t, )
// This block of code is generated, do not edit it directly.

- (void)writeSInt32Array:(int32_t)fieldNumber
                  values:(GPBInt32Array *)values
                     tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeSInt32SizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeSInt32NoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeSInt32:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SInt64, Int64, int64_t, )
// This block of code is generated, do not edit it directly.

- (void)writeSInt64Array:(int32_t)fieldNumber
                  values:(GPBInt64Array *)values
                     tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeSInt64SizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeSInt64NoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeSInt64:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SFixed64, Int64, int64_t, )
// This block of code is generated, do not edit it directly.

- (void)writeSFixed64Array:(int32_t)fieldNumber
                    values:(GPBInt64Array *)values
                       tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeSFixed64SizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeSFixed64NoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeSFixed64:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SFixed32, Int32, int32_t, )
// This block of code is generated, do not edit it directly.

- (void)writeSFixed32Array:(int32_t)fieldNumber
                    values:(GPBInt32Array *)values
                       tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeSFixed32SizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeSFixed32NoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeSFixed32:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Bool, Bool, BOOL, )
// This block of code is generated, do not edit it directly.

- (void)writeBoolArray:(int32_t)fieldNumber
                values:(GPBBoolArray *)values
                   tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeBoolSizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeBoolNoTag:value];
    }];
  } else {
    [values enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeBool:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Enum, Enum, int32_t, Raw)
// This block of code is generated, do not edit it directly.

- (void)writeEnumArray:(int32_t)fieldNumber
                values:(GPBEnumArray *)values
                   tag:(uint32_t)tag {
  if (tag != 0) {
    if (values.count == 0) return;
    __block size_t dataSize = 0;
    [values enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      dataSize += GPBComputeEnumSizeNoTag(value);
    }];
    GPBWriteRawVarint32(&state_, tag);
    GPBWriteRawVarint32(&state_, (int32_t)dataSize);
    [values enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeEnumNoTag:value];
    }];
  } else {
    [values enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
#pragma unused(idx, stop)
      [self writeEnum:fieldNumber value:value];
    }];
  }
}

//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(String, NSString)
// This block of code is generated, do not edit it directly.

- (void)writeStringArray:(int32_t)fieldNumber values:(NSArray *)values {
  for (NSString *value in values) {
    [self writeString:fieldNumber value:value];
  }
}

//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Message, GPBMessage)
// This block of code is generated, do not edit it directly.

- (void)writeMessageArray:(int32_t)fieldNumber values:(NSArray *)values {
  for (GPBMessage *value in values) {
    [self writeMessage:fieldNumber value:value];
  }
}

//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Bytes, NSData)
// This block of code is generated, do not edit it directly.

- (void)writeBytesArray:(int32_t)fieldNumber values:(NSArray *)values {
  for (NSData *value in values) {
    [self writeBytes:fieldNumber value:value];
  }
}

//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Group, GPBMessage)
// This block of code is generated, do not edit it directly.

- (void)writeGroupArray:(int32_t)fieldNumber values:(NSArray *)values {
  for (GPBMessage *value in values) {
    [self writeGroup:fieldNumber value:value];
  }
}

//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(UnknownGroup, GPBUnknownFieldSet)
// This block of code is generated, do not edit it directly.

- (void)writeUnknownGroupArray:(int32_t)fieldNumber values:(NSArray *)values {
  for (GPBUnknownFieldSet *value in values) {
    [self writeUnknownGroup:fieldNumber value:value];
  }
}

//%PDDM-EXPAND-END (19 expansions)

- (void)writeMessageSetExtension:(int32_t)fieldNumber
                           value:(GPBMessage *)value {
  GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
                        GPBWireFormatStartGroup);
  GPBWriteUInt32(&state_, GPBWireFormatMessageSetTypeId, fieldNumber);
  [self writeMessage:GPBWireFormatMessageSetMessage value:value];
  GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
                        GPBWireFormatEndGroup);
}

- (void)writeRawMessageSetExtension:(int32_t)fieldNumber value:(NSData *)value {
  GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
                        GPBWireFormatStartGroup);
  GPBWriteUInt32(&state_, GPBWireFormatMessageSetTypeId, fieldNumber);
  [self writeBytes:GPBWireFormatMessageSetMessage value:value];
  GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
                        GPBWireFormatEndGroup);
}

- (void)flush {
  if (state_.output != nil) {
    GPBRefreshBuffer(&state_);
  }
}

- (void)writeRawByte:(uint8_t)value {
  GPBWriteRawByte(&state_, value);
}

- (void)writeRawData:(const NSData *)data {
  [self writeRawPtr:[data bytes] offset:0 length:[data length]];
}

- (void)writeRawPtr:(const void *)value
             offset:(size_t)offset
             length:(size_t)length {
  if (value == nil || length == 0) {
    return;
  }

  NSUInteger bufferLength = state_.size;
  NSUInteger bufferBytesLeft = bufferLength - state_.position;
  if (bufferBytesLeft >= length) {
    // We have room in the current buffer.
    memcpy(state_.bytes + state_.position, ((uint8_t *)value) + offset, length);
    state_.position += length;
  } else {
    // Write extends past current buffer.  Fill the rest of this buffer and
    // flush.
    size_t bytesWritten = bufferBytesLeft;
    memcpy(state_.bytes + state_.position, ((uint8_t *)value) + offset,
           bytesWritten);
    offset += bytesWritten;
    length -= bytesWritten;
    state_.position = bufferLength;
    GPBRefreshBuffer(&state_);
    bufferLength = state_.size;

    // Now deal with the rest.
    // Since we have an output stream, this is our buffer
    // and buffer offset == 0
    if (length <= bufferLength) {
      // Fits in new buffer.
      memcpy(state_.bytes, ((uint8_t *)value) + offset, length);
      state_.position = length;
    } else {
      // Write is very big.  Let's do it all at once.
      [state_.output write:((uint8_t *)value) + offset maxLength:length];
    }
  }
}

- (void)writeTag:(uint32_t)fieldNumber format:(GPBWireFormat)format {
  GPBWriteTagWithFormat(&state_, fieldNumber, format);
}

- (void)writeRawVarint32:(int32_t)value {
  GPBWriteRawVarint32(&state_, value);
}

- (void)writeRawVarintSizeTAs32:(size_t)value {
  // Note the truncation.
  GPBWriteRawVarint32(&state_, (int32_t)value);
}

- (void)writeRawVarint64:(int64_t)value {
  GPBWriteRawVarint64(&state_, value);
}

- (void)writeRawLittleEndian32:(int32_t)value {
  GPBWriteRawLittleEndian32(&state_, value);
}

- (void)writeRawLittleEndian64:(int64_t)value {
  GPBWriteRawLittleEndian64(&state_, value);
}

#pragma clang diagnostic pop

@end

size_t GPBComputeDoubleSizeNoTag(Float64 value) {
#pragma unused(value)
  return LITTLE_ENDIAN_64_SIZE;
}

size_t GPBComputeFloatSizeNoTag(Float32 value) {
#pragma unused(value)
  return LITTLE_ENDIAN_32_SIZE;
}

size_t GPBComputeUInt64SizeNoTag(uint64_t value) {
  return GPBComputeRawVarint64Size(value);
}

size_t GPBComputeInt64SizeNoTag(int64_t value) {
  return GPBComputeRawVarint64Size(value);
}

size_t GPBComputeInt32SizeNoTag(int32_t value) {
  if (value >= 0) {
    return GPBComputeRawVarint32Size(value);
  } else {
    // Must sign-extend.
    return 10;
  }
}

size_t GPBComputeSizeTSizeAsInt32NoTag(size_t value) {
  return GPBComputeInt32SizeNoTag((int32_t)value);
}

size_t GPBComputeFixed64SizeNoTag(uint64_t value) {
#pragma unused(value)
  return LITTLE_ENDIAN_64_SIZE;
}

size_t GPBComputeFixed32SizeNoTag(uint32_t value) {
#pragma unused(value)
  return LITTLE_ENDIAN_32_SIZE;
}

size_t GPBComputeBoolSizeNoTag(BOOL value) {
#pragma unused(value)
  return 1;
}

size_t GPBComputeStringSizeNoTag(NSString *value) {
  NSUInteger length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  return GPBComputeRawVarint32SizeForInteger(length) + length;
}

size_t GPBComputeGroupSizeNoTag(GPBMessage *value) {
  return [value serializedSize];
}

size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value) {
  return value.serializedSize;
}

size_t GPBComputeMessageSizeNoTag(GPBMessage *value) {
  size_t size = [value serializedSize];
  return GPBComputeRawVarint32SizeForInteger(size) + size;
}

size_t GPBComputeBytesSizeNoTag(NSData *value) {
  NSUInteger valueLength = [value length];
  return GPBComputeRawVarint32SizeForInteger(valueLength) + valueLength;
}

size_t GPBComputeUInt32SizeNoTag(int32_t value) {
  return GPBComputeRawVarint32Size(value);
}

size_t GPBComputeEnumSizeNoTag(int32_t value) {
  return GPBComputeRawVarint32Size(value);
}

size_t GPBComputeSFixed32SizeNoTag(int32_t value) {
#pragma unused(value)
  return LITTLE_ENDIAN_32_SIZE;
}

size_t GPBComputeSFixed64SizeNoTag(int64_t value) {
#pragma unused(value)
  return LITTLE_ENDIAN_64_SIZE;
}

size_t GPBComputeSInt32SizeNoTag(int32_t value) {
  return GPBComputeRawVarint32Size(GPBEncodeZigZag32(value));
}

size_t GPBComputeSInt64SizeNoTag(int64_t value) {
  return GPBComputeRawVarint64Size(GPBEncodeZigZag64(value));
}

size_t GPBComputeDoubleSize(int32_t fieldNumber, double value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeDoubleSizeNoTag(value);
}

size_t GPBComputeFloatSize(int32_t fieldNumber, float value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeFloatSizeNoTag(value);
}

size_t GPBComputeUInt64Size(int32_t fieldNumber, uint64_t value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeUInt64SizeNoTag(value);
}

size_t GPBComputeInt64Size(int32_t fieldNumber, int64_t value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeInt64SizeNoTag(value);
}

size_t GPBComputeInt32Size(int32_t fieldNumber, int32_t value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeInt32SizeNoTag(value);
}

size_t GPBComputeFixed64Size(int32_t fieldNumber, uint64_t value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeFixed64SizeNoTag(value);
}

size_t GPBComputeFixed32Size(int32_t fieldNumber, uint32_t value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeFixed32SizeNoTag(value);
}

size_t GPBComputeBoolSize(int32_t fieldNumber, BOOL value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeBoolSizeNoTag(value);
}

size_t GPBComputeStringSize(int32_t fieldNumber, NSString *value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeStringSizeNoTag(value);
}

size_t GPBComputeGroupSize(int32_t fieldNumber, GPBMessage *value) {
  return GPBComputeTagSize(fieldNumber) * 2 + GPBComputeGroupSizeNoTag(value);
}

size_t GPBComputeUnknownGroupSize(int32_t fieldNumber,
                                  GPBUnknownFieldSet *value) {
  return GPBComputeTagSize(fieldNumber) * 2 +
         GPBComputeUnknownGroupSizeNoTag(value);
}

size_t GPBComputeMessageSize(int32_t fieldNumber, GPBMessage *value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeMessageSizeNoTag(value);
}

size_t GPBComputeBytesSize(int32_t fieldNumber, NSData *value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeBytesSizeNoTag(value);
}

size_t GPBComputeUInt32Size(int32_t fieldNumber, uint32_t value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeUInt32SizeNoTag(value);
}

size_t GPBComputeEnumSize(int32_t fieldNumber, int32_t value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeEnumSizeNoTag(value);
}

size_t GPBComputeSFixed32Size(int32_t fieldNumber, int32_t value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeSFixed32SizeNoTag(value);
}

size_t GPBComputeSFixed64Size(int32_t fieldNumber, int64_t value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeSFixed64SizeNoTag(value);
}

size_t GPBComputeSInt32Size(int32_t fieldNumber, int32_t value) {
  return GPBComputeTagSize(fieldNumber) + GPBComputeSInt32SizeNoTag(value);
}

size_t GPBComputeSInt64Size(int32_t fieldNumber, int64_t value) {
  return GPBComputeTagSize(fieldNumber) +
         GPBComputeRawVarint64Size(GPBEncodeZigZag64(value));
}

size_t GPBComputeMessageSetExtensionSize(int32_t fieldNumber,
                                         GPBMessage *value) {
  return GPBComputeTagSize(GPBWireFormatMessageSetItem) * 2 +
         GPBComputeUInt32Size(GPBWireFormatMessageSetTypeId, fieldNumber) +
         GPBComputeMessageSize(GPBWireFormatMessageSetMessage, value);
}

size_t GPBComputeRawMessageSetExtensionSize(int32_t fieldNumber,
                                            NSData *value) {
  return GPBComputeTagSize(GPBWireFormatMessageSetItem) * 2 +
         GPBComputeUInt32Size(GPBWireFormatMessageSetTypeId, fieldNumber) +
         GPBComputeBytesSize(GPBWireFormatMessageSetMessage, value);
}

size_t GPBComputeTagSize(int32_t fieldNumber) {
  return GPBComputeRawVarint32Size(
      GPBWireFormatMakeTag(fieldNumber, GPBWireFormatVarint));
}

size_t GPBComputeWireFormatTagSize(int field_number, GPBDataType dataType) {
  size_t result = GPBComputeTagSize(field_number);
  if (dataType == GPBDataTypeGroup) {
    // Groups have both a start and an end tag.
    return result * 2;
  } else {
    return result;
  }
}

size_t GPBComputeRawVarint32Size(int32_t value) {
  // value is treated as unsigned, so it won't be sign-extended if negative.
  if ((value & (0xffffffff << 7)) == 0) return 1;
  if ((value & (0xffffffff << 14)) == 0) return 2;
  if ((value & (0xffffffff << 21)) == 0) return 3;
  if ((value & (0xffffffff << 28)) == 0) return 4;
  return 5;
}

size_t GPBComputeRawVarint32SizeForInteger(NSInteger value) {
  // Note the truncation.
  return GPBComputeRawVarint32Size((int32_t)value);
}

size_t GPBComputeRawVarint64Size(int64_t value) {
  if ((value & (0xffffffffffffffffL << 7)) == 0) return 1;
  if ((value & (0xffffffffffffffffL << 14)) == 0) return 2;
  if ((value & (0xffffffffffffffffL << 21)) == 0) return 3;
  if ((value & (0xffffffffffffffffL << 28)) == 0) return 4;
  if ((value & (0xffffffffffffffffL << 35)) == 0) return 5;
  if ((value & (0xffffffffffffffffL << 42)) == 0) return 6;
  if ((value & (0xffffffffffffffffL << 49)) == 0) return 7;
  if ((value & (0xffffffffffffffffL << 56)) == 0) return 8;
  if ((value & (0xffffffffffffffffL << 63)) == 0) return 9;
  return 10;
}