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
|
using System;
using System.IO;
using System.Text;
using ProtoBuf.Meta;
namespace ProtoBuf
{
public sealed class ProtoWriter : IDisposable
{
internal NetObjectCache NetCache
{
get
{
return this.netCache;
}
}
internal WireType WireType
{
get
{
return this.wireType;
}
}
public SerializationContext Context
{
get
{
return this.context;
}
}
public TypeModel Model
{
get
{
return this.model;
}
}
private Stream dest;
private TypeModel model;
private readonly NetObjectCache netCache = new NetObjectCache();
private int fieldNumber;
private int flushLock;
private WireType wireType;
private int depth = 0;
private const int RecursionCheckDepth = 25;
private MutableList recursionStack;
private readonly SerializationContext context;
private byte[] ioBuffer;
private int ioIndex;
private int position;
private static readonly UTF8Encoding encoding = new UTF8Encoding();
private int packedFieldNumber;
public static void WriteObject(object value, int key, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
bool flag2 = writer.model == null;
if (flag2)
{
throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
}
SubItemToken token = ProtoWriter.StartSubItem(value, writer);
bool flag3 = key >= 0;
if (flag3)
{
writer.model.Serialize(key, value, writer);
}
else
{
bool flag4 = writer.model != null && writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, 1, value, false);
if (!flag4)
{
TypeModel.ThrowUnexpectedType(value.GetType());
}
}
ProtoWriter.EndSubItem(token, writer);
}
public static void WriteRecursionSafeObject(object value, int key, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
bool flag2 = writer.model == null;
if (flag2)
{
throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
}
SubItemToken token = ProtoWriter.StartSubItem(null, writer);
writer.model.Serialize(key, value, writer);
ProtoWriter.EndSubItem(token, writer);
}
internal static void WriteObject(object value, int key, ProtoWriter writer, PrefixStyle style, int fieldNumber)
{
bool flag = writer.model == null;
if (flag)
{
throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
}
bool flag2 = writer.wireType != WireType.None;
if (flag2)
{
throw ProtoWriter.CreateException(writer);
}
if (style != PrefixStyle.Base128)
{
if (style - PrefixStyle.Fixed32 > 1)
{
throw new ArgumentOutOfRangeException("style");
}
writer.fieldNumber = 0;
writer.wireType = WireType.Fixed32;
}
else
{
writer.wireType = WireType.String;
writer.fieldNumber = fieldNumber;
bool flag3 = fieldNumber > 0;
if (flag3)
{
ProtoWriter.WriteHeaderCore(fieldNumber, WireType.String, writer);
}
}
SubItemToken token = ProtoWriter.StartSubItem(value, writer, true);
bool flag4 = key < 0;
if (flag4)
{
bool flag5 = !writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, 1, value, false);
if (flag5)
{
TypeModel.ThrowUnexpectedType(value.GetType());
}
}
else
{
writer.model.Serialize(key, value, writer);
}
ProtoWriter.EndSubItem(token, writer, style);
}
internal int GetTypeKey(ref Type type)
{
return this.model.GetKey(ref type);
}
public static void WriteFieldHeader(int fieldNumber, WireType wireType, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
bool flag2 = writer.wireType != WireType.None;
if (flag2)
{
throw new InvalidOperationException(string.Concat(new string[]
{
"Cannot write a ",
wireType.ToString(),
" header until the ",
writer.wireType.ToString(),
" data has been written"
}));
}
bool flag3 = fieldNumber < 0;
if (flag3)
{
throw new ArgumentOutOfRangeException("fieldNumber");
}
switch (wireType)
{
case WireType.Variant:
case WireType.Fixed64:
case WireType.String:
case WireType.StartGroup:
case WireType.Fixed32:
case WireType.SignedVariant:
{
bool flag4 = writer.packedFieldNumber == 0;
if (flag4)
{
writer.fieldNumber = fieldNumber;
writer.wireType = wireType;
ProtoWriter.WriteHeaderCore(fieldNumber, wireType, writer);
}
else
{
bool flag5 = writer.packedFieldNumber == fieldNumber;
if (!flag5)
{
throw new InvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber.ToString() + " but received " + fieldNumber.ToString());
}
WireType wireType2 = wireType;
if (wireType2 > WireType.Fixed64 && wireType2 != WireType.Fixed32 && wireType2 != WireType.SignedVariant)
{
throw new InvalidOperationException("Wire-type cannot be encoded as packed: " + wireType.ToString());
}
writer.fieldNumber = fieldNumber;
writer.wireType = wireType;
}
return;
}
}
throw new ArgumentException("Invalid wire-type: " + wireType.ToString(), "wireType");
}
internal static void WriteHeaderCore(int fieldNumber, WireType wireType, ProtoWriter writer)
{
uint value = (uint)(fieldNumber << 3 | (int)(wireType & (WireType)7));
ProtoWriter.WriteUInt32Variant(value, writer);
}
public static void WriteBytes(byte[] data, ProtoWriter writer)
{
bool flag = data == null;
if (flag)
{
throw new ArgumentNullException("data");
}
ProtoWriter.WriteBytes(data, 0, data.Length, writer);
}
public static void WriteBytes(byte[] data, int offset, int length, ProtoWriter writer)
{
bool flag = data == null;
if (flag)
{
throw new ArgumentNullException("data");
}
bool flag2 = writer == null;
if (flag2)
{
throw new ArgumentNullException("writer");
}
switch (writer.wireType)
{
case WireType.Fixed64:
{
bool flag3 = length != 8;
if (flag3)
{
throw new ArgumentException("length");
}
goto IL_EE;
}
case WireType.String:
{
ProtoWriter.WriteUInt32Variant((uint)length, writer);
writer.wireType = WireType.None;
bool flag4 = length == 0;
if (flag4)
{
return;
}
bool flag5 = writer.flushLock != 0 || length <= writer.ioBuffer.Length;
if (flag5)
{
goto IL_EE;
}
ProtoWriter.Flush(writer);
writer.dest.Write(data, offset, length);
writer.position += length;
return;
}
case WireType.Fixed32:
{
bool flag6 = length != 4;
if (flag6)
{
throw new ArgumentException("length");
}
goto IL_EE;
}
}
throw ProtoWriter.CreateException(writer);
IL_EE:
ProtoWriter.DemandSpace(length, writer);
Helpers.BlockCopy(data, offset, writer.ioBuffer, writer.ioIndex, length);
ProtoWriter.IncrementedAndReset(length, writer);
}
private static void CopyRawFromStream(Stream source, ProtoWriter writer)
{
byte[] array = writer.ioBuffer;
int num = array.Length - writer.ioIndex;
int num2 = 1;
while (num > 0 && (num2 = source.Read(array, writer.ioIndex, num)) > 0)
{
writer.ioIndex += num2;
writer.position += num2;
num -= num2;
}
bool flag = num2 <= 0;
if (!flag)
{
bool flag2 = writer.flushLock == 0;
if (flag2)
{
ProtoWriter.Flush(writer);
while ((num2 = source.Read(array, 0, array.Length)) > 0)
{
writer.dest.Write(array, 0, num2);
writer.position += num2;
}
}
else
{
for (;;)
{
ProtoWriter.DemandSpace(128, writer);
bool flag3 = (num2 = source.Read(writer.ioBuffer, writer.ioIndex, writer.ioBuffer.Length - writer.ioIndex)) <= 0;
if (flag3)
{
break;
}
writer.position += num2;
writer.ioIndex += num2;
}
}
}
}
private static void IncrementedAndReset(int length, ProtoWriter writer)
{
Helpers.DebugAssert(length >= 0);
writer.ioIndex += length;
writer.position += length;
writer.wireType = WireType.None;
}
public static SubItemToken StartSubItem(object instance, ProtoWriter writer)
{
return ProtoWriter.StartSubItem(instance, writer, false);
}
private void CheckRecursionStackAndPush(object instance)
{
bool flag = this.recursionStack == null;
if (flag)
{
this.recursionStack = new MutableList();
}
else
{
int num = 0;
bool flag2 = instance != null && (num = this.recursionStack.IndexOfReference(instance)) >= 0;
if (flag2)
{
Helpers.DebugWriteLine("Stack:");
foreach (object obj in this.recursionStack)
{
Helpers.DebugWriteLine((obj == null) ? "<null>" : obj.ToString());
}
Helpers.DebugWriteLine((instance == null) ? "<null>" : instance.ToString());
throw new ProtoException("Possible recursion detected (offset: " + (this.recursionStack.Count - num).ToString() + " level(s)): " + instance.ToString());
}
}
this.recursionStack.Add(instance);
}
private void PopRecursionStack()
{
this.recursionStack.RemoveLast();
}
private static SubItemToken StartSubItem(object instance, ProtoWriter writer, bool allowFixed)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
int num = writer.depth + 1;
writer.depth = num;
bool flag2 = num > 25;
if (flag2)
{
writer.CheckRecursionStackAndPush(instance);
}
bool flag3 = writer.packedFieldNumber != 0;
if (flag3)
{
throw new InvalidOperationException("Cannot begin a sub-item while performing packed encoding");
}
switch (writer.wireType)
{
case WireType.String:
{
bool flag4 = writer.model != null && writer.model.ForwardsOnly;
if (flag4)
{
throw new ProtoException("Should not be buffering data");
}
writer.wireType = WireType.None;
ProtoWriter.DemandSpace(32, writer);
writer.flushLock++;
writer.position++;
num = writer.ioIndex;
writer.ioIndex = num + 1;
return new SubItemToken(num);
}
case WireType.StartGroup:
writer.wireType = WireType.None;
return new SubItemToken(-writer.fieldNumber);
case WireType.Fixed32:
{
bool flag5 = !allowFixed;
if (flag5)
{
throw ProtoWriter.CreateException(writer);
}
ProtoWriter.DemandSpace(32, writer);
writer.flushLock++;
SubItemToken result = new SubItemToken(writer.ioIndex);
ProtoWriter.IncrementedAndReset(4, writer);
return result;
}
}
throw ProtoWriter.CreateException(writer);
}
public static void EndSubItem(SubItemToken token, ProtoWriter writer)
{
ProtoWriter.EndSubItem(token, writer, PrefixStyle.Base128);
}
private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
bool flag2 = writer.wireType != WireType.None;
if (flag2)
{
throw ProtoWriter.CreateException(writer);
}
int value = token.value;
bool flag3 = writer.depth <= 0;
if (flag3)
{
throw ProtoWriter.CreateException(writer);
}
int num = writer.depth;
writer.depth = num - 1;
bool flag4 = num > 25;
if (flag4)
{
writer.PopRecursionStack();
}
writer.packedFieldNumber = 0;
bool flag5 = value < 0;
if (flag5)
{
ProtoWriter.WriteHeaderCore(-value, WireType.EndGroup, writer);
writer.wireType = WireType.None;
}
else
{
switch (style)
{
case PrefixStyle.Base128:
{
int num2 = writer.ioIndex - value - 1;
int num3 = 0;
uint num4 = (uint)num2;
while ((num4 >>= 7) > 0u)
{
num3++;
}
bool flag6 = num3 == 0;
if (flag6)
{
writer.ioBuffer[value] = (byte)(num2 & 127);
}
else
{
ProtoWriter.DemandSpace(num3, writer);
byte[] array = writer.ioBuffer;
Helpers.BlockCopy(array, value + 1, array, value + 1 + num3, num2);
num4 = (uint)num2;
do
{
array[value++] = (byte)((num4 & 127u) | 128u);
}
while ((num4 >>= 7) > 0u);
array[value - 1] = (byte)((int)array[value - 1] & -129);
writer.position += num3;
writer.ioIndex += num3;
}
break;
}
case PrefixStyle.Fixed32:
{
int num2 = writer.ioIndex - value - 4;
ProtoWriter.WriteInt32ToBuffer(num2, writer.ioBuffer, value);
break;
}
case PrefixStyle.Fixed32BigEndian:
{
int num2 = writer.ioIndex - value - 4;
byte[] array2 = writer.ioBuffer;
ProtoWriter.WriteInt32ToBuffer(num2, array2, value);
byte b = array2[value];
array2[value] = array2[value + 3];
array2[value + 3] = b;
b = array2[value + 1];
array2[value + 1] = array2[value + 2];
array2[value + 2] = b;
break;
}
default:
throw new ArgumentOutOfRangeException("style");
}
num = writer.flushLock - 1;
writer.flushLock = num;
bool flag7 = num == 0 && writer.ioIndex >= 1024;
if (flag7)
{
ProtoWriter.Flush(writer);
}
}
}
public ProtoWriter(Stream dest, TypeModel model, SerializationContext context)
{
bool flag = dest == null;
if (flag)
{
throw new ArgumentNullException("dest");
}
bool flag2 = !dest.CanWrite;
if (flag2)
{
throw new ArgumentException("Cannot write to stream", "dest");
}
this.dest = dest;
this.ioBuffer = BufferPool.GetBuffer();
this.model = model;
this.wireType = WireType.None;
bool flag3 = context == null;
if (flag3)
{
context = SerializationContext.Default;
}
else
{
context.Freeze();
}
this.context = context;
}
void IDisposable.Dispose()
{
this.Dispose();
}
private void Dispose()
{
bool flag = this.dest != null;
if (flag)
{
ProtoWriter.Flush(this);
this.dest = null;
}
this.model = null;
BufferPool.ReleaseBufferToPool(ref this.ioBuffer);
}
internal static int GetPosition(ProtoWriter writer)
{
return writer.position;
}
private static void DemandSpace(int required, ProtoWriter writer)
{
bool flag = writer.ioBuffer.Length - writer.ioIndex < required;
if (flag)
{
bool flag2 = writer.flushLock == 0;
if (flag2)
{
ProtoWriter.Flush(writer);
bool flag3 = writer.ioBuffer.Length - writer.ioIndex >= required;
if (flag3)
{
return;
}
}
BufferPool.ResizeAndFlushLeft(ref writer.ioBuffer, required + writer.ioIndex, 0, writer.ioIndex);
}
}
public void Close()
{
bool flag = this.depth != 0 || this.flushLock != 0;
if (flag)
{
throw new InvalidOperationException("Unable to close stream in an incomplete state");
}
this.Dispose();
}
internal void CheckDepthFlushlock()
{
bool flag = this.depth != 0 || this.flushLock != 0;
if (flag)
{
throw new InvalidOperationException("The writer is in an incomplete state");
}
}
internal static void Flush(ProtoWriter writer)
{
bool flag = writer.flushLock == 0 && writer.ioIndex != 0;
if (flag)
{
writer.dest.Write(writer.ioBuffer, 0, writer.ioIndex);
writer.ioIndex = 0;
}
}
private static void WriteUInt32Variant(uint value, ProtoWriter writer)
{
ProtoWriter.DemandSpace(5, writer);
int num = 0;
do
{
byte[] array = writer.ioBuffer;
int num2 = writer.ioIndex;
writer.ioIndex = num2 + 1;
array[num2] = (byte)((value & 127u) | 128u);
num++;
}
while ((value >>= 7) > 0u);
byte[] array2 = writer.ioBuffer;
int num3 = writer.ioIndex - 1;
array2[num3] &= 127;
writer.position += num;
}
internal static uint Zig(int value)
{
return (uint)(value << 1 ^ value >> 31);
}
internal static ulong Zig(long value)
{
return (ulong)(value << 1 ^ value >> 63);
}
private static void WriteUInt64Variant(ulong value, ProtoWriter writer)
{
ProtoWriter.DemandSpace(10, writer);
int num = 0;
do
{
byte[] array = writer.ioBuffer;
int num2 = writer.ioIndex;
writer.ioIndex = num2 + 1;
array[num2] = (byte)((value & 127UL) | 128UL);
num++;
}
while ((value >>= 7) > 0UL);
byte[] array2 = writer.ioBuffer;
int num3 = writer.ioIndex - 1;
array2[num3] &= 127;
writer.position += num;
}
public static void WriteString(string value, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
bool flag2 = writer.wireType != WireType.String;
if (flag2)
{
throw ProtoWriter.CreateException(writer);
}
bool flag3 = value == null;
if (flag3)
{
throw new ArgumentNullException("value");
}
int length = value.Length;
bool flag4 = length == 0;
if (flag4)
{
ProtoWriter.WriteUInt32Variant(0u, writer);
writer.wireType = WireType.None;
}
else
{
int byteCount = ProtoWriter.encoding.GetByteCount(value);
ProtoWriter.WriteUInt32Variant((uint)byteCount, writer);
ProtoWriter.DemandSpace(byteCount, writer);
int bytes = ProtoWriter.encoding.GetBytes(value, 0, value.Length, writer.ioBuffer, writer.ioIndex);
Helpers.DebugAssert(byteCount == bytes);
ProtoWriter.IncrementedAndReset(bytes, writer);
}
}
public static void WriteUInt64(ulong value, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
WireType wireType = writer.wireType;
if (wireType != WireType.Variant)
{
if (wireType != WireType.Fixed64)
{
if (wireType != WireType.Fixed32)
{
throw ProtoWriter.CreateException(writer);
}
ProtoWriter.WriteUInt32(checked((uint)value), writer);
}
else
{
ProtoWriter.WriteInt64((long)value, writer);
}
}
else
{
ProtoWriter.WriteUInt64Variant(value, writer);
writer.wireType = WireType.None;
}
}
public static void WriteInt64(long value, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
WireType wireType = writer.wireType;
if (wireType <= WireType.Fixed64)
{
if (wireType == WireType.Variant)
{
bool flag2 = value >= 0L;
if (flag2)
{
ProtoWriter.WriteUInt64Variant((ulong)value, writer);
writer.wireType = WireType.None;
}
else
{
ProtoWriter.DemandSpace(10, writer);
byte[] array = writer.ioBuffer;
int num = writer.ioIndex;
array[num] = (byte)(value | 128L);
array[num + 1] = (byte)((int)(value >> 7) | 128);
array[num + 2] = (byte)((int)(value >> 14) | 128);
array[num + 3] = (byte)((int)(value >> 21) | 128);
array[num + 4] = (byte)((int)(value >> 28) | 128);
array[num + 5] = (byte)((int)(value >> 35) | 128);
array[num + 6] = (byte)((int)(value >> 42) | 128);
array[num + 7] = (byte)((int)(value >> 49) | 128);
array[num + 8] = (byte)((int)(value >> 56) | 128);
array[num + 9] = 1;
ProtoWriter.IncrementedAndReset(10, writer);
}
return;
}
if (wireType == WireType.Fixed64)
{
ProtoWriter.DemandSpace(8, writer);
byte[] array = writer.ioBuffer;
int num = writer.ioIndex;
array[num] = (byte)value;
array[num + 1] = (byte)(value >> 8);
array[num + 2] = (byte)(value >> 16);
array[num + 3] = (byte)(value >> 24);
array[num + 4] = (byte)(value >> 32);
array[num + 5] = (byte)(value >> 40);
array[num + 6] = (byte)(value >> 48);
array[num + 7] = (byte)(value >> 56);
ProtoWriter.IncrementedAndReset(8, writer);
return;
}
}
else
{
if (wireType == WireType.Fixed32)
{
ProtoWriter.WriteInt32(checked((int)value), writer);
return;
}
if (wireType == WireType.SignedVariant)
{
ProtoWriter.WriteUInt64Variant(ProtoWriter.Zig(value), writer);
writer.wireType = WireType.None;
return;
}
}
throw ProtoWriter.CreateException(writer);
}
public static void WriteUInt32(uint value, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
WireType wireType = writer.wireType;
if (wireType != WireType.Variant)
{
if (wireType != WireType.Fixed64)
{
if (wireType != WireType.Fixed32)
{
throw ProtoWriter.CreateException(writer);
}
ProtoWriter.WriteInt32((int)value, writer);
}
else
{
ProtoWriter.WriteInt64((long)value, writer);
}
}
else
{
ProtoWriter.WriteUInt32Variant(value, writer);
writer.wireType = WireType.None;
}
}
public static void WriteInt16(short value, ProtoWriter writer)
{
ProtoWriter.WriteInt32((int)value, writer);
}
public static void WriteUInt16(ushort value, ProtoWriter writer)
{
ProtoWriter.WriteUInt32((uint)value, writer);
}
public static void WriteByte(byte value, ProtoWriter writer)
{
ProtoWriter.WriteUInt32((uint)value, writer);
}
public static void WriteSByte(sbyte value, ProtoWriter writer)
{
ProtoWriter.WriteInt32((int)value, writer);
}
private static void WriteInt32ToBuffer(int value, byte[] buffer, int index)
{
buffer[index] = (byte)value;
buffer[index + 1] = (byte)(value >> 8);
buffer[index + 2] = (byte)(value >> 16);
buffer[index + 3] = (byte)(value >> 24);
}
public static void WriteInt32(int value, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
WireType wireType = writer.wireType;
if (wireType <= WireType.Fixed64)
{
if (wireType == WireType.Variant)
{
bool flag2 = value >= 0;
if (flag2)
{
ProtoWriter.WriteUInt32Variant((uint)value, writer);
writer.wireType = WireType.None;
}
else
{
ProtoWriter.DemandSpace(10, writer);
byte[] array = writer.ioBuffer;
int num = writer.ioIndex;
array[num] = (byte)(value | 128);
array[num + 1] = (byte)(value >> 7 | 128);
array[num + 2] = (byte)(value >> 14 | 128);
array[num + 3] = (byte)(value >> 21 | 128);
array[num + 4] = (byte)(value >> 28 | 128);
array[num + 5] = (array[num + 6] = (array[num + 7] = (array[num + 8] = byte.MaxValue)));
array[num + 9] = 1;
ProtoWriter.IncrementedAndReset(10, writer);
}
return;
}
if (wireType == WireType.Fixed64)
{
ProtoWriter.DemandSpace(8, writer);
byte[] array = writer.ioBuffer;
int num = writer.ioIndex;
array[num] = (byte)value;
array[num + 1] = (byte)(value >> 8);
array[num + 2] = (byte)(value >> 16);
array[num + 3] = (byte)(value >> 24);
array[num + 4] = (array[num + 5] = (array[num + 6] = (array[num + 7] = 0)));
ProtoWriter.IncrementedAndReset(8, writer);
return;
}
}
else
{
if (wireType == WireType.Fixed32)
{
ProtoWriter.DemandSpace(4, writer);
ProtoWriter.WriteInt32ToBuffer(value, writer.ioBuffer, writer.ioIndex);
ProtoWriter.IncrementedAndReset(4, writer);
return;
}
if (wireType == WireType.SignedVariant)
{
ProtoWriter.WriteUInt32Variant(ProtoWriter.Zig(value), writer);
writer.wireType = WireType.None;
return;
}
}
throw ProtoWriter.CreateException(writer);
}
public unsafe static void WriteDouble(double value, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
WireType wireType = writer.wireType;
if (wireType != WireType.Fixed64)
{
if (wireType != WireType.Fixed32)
{
throw ProtoWriter.CreateException(writer);
}
float value2 = (float)value;
bool flag2 = Helpers.IsInfinity(value2) && !Helpers.IsInfinity(value);
if (flag2)
{
throw new OverflowException();
}
ProtoWriter.WriteSingle(value2, writer);
}
else
{
ProtoWriter.WriteInt64(*(long*)(&value), writer);
}
}
public unsafe static void WriteSingle(float value, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
WireType wireType = writer.wireType;
if (wireType != WireType.Fixed64)
{
if (wireType != WireType.Fixed32)
{
throw ProtoWriter.CreateException(writer);
}
ProtoWriter.WriteInt32(*(int*)(&value), writer);
}
else
{
ProtoWriter.WriteDouble((double)value, writer);
}
}
public static void ThrowEnumException(ProtoWriter writer, object enumValue)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
string str = (enumValue == null) ? "<null>" : (enumValue.GetType().FullName + "." + enumValue.ToString());
throw new ProtoException("No wire-value is mapped to the enum " + str + " at position " + writer.position.ToString());
}
internal static Exception CreateException(ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
return new ProtoException("Invalid serialization operation with wire-type " + writer.wireType.ToString() + " at position " + writer.position.ToString());
}
public static void WriteBoolean(bool value, ProtoWriter writer)
{
ProtoWriter.WriteUInt32(value ? 1u : 0u, writer);
}
public static void AppendExtensionData(IExtensible instance, ProtoWriter writer)
{
bool flag = instance == null;
if (flag)
{
throw new ArgumentNullException("instance");
}
bool flag2 = writer == null;
if (flag2)
{
throw new ArgumentNullException("writer");
}
bool flag3 = writer.wireType != WireType.None;
if (flag3)
{
throw ProtoWriter.CreateException(writer);
}
IExtension extensionObject = instance.GetExtensionObject(false);
bool flag4 = extensionObject != null;
if (flag4)
{
Stream stream = extensionObject.BeginQuery();
try
{
ProtoWriter.CopyRawFromStream(stream, writer);
}
finally
{
extensionObject.EndQuery(stream);
}
}
}
public static void SetPackedField(int fieldNumber, ProtoWriter writer)
{
bool flag = fieldNumber <= 0;
if (flag)
{
throw new ArgumentOutOfRangeException("fieldNumber");
}
bool flag2 = writer == null;
if (flag2)
{
throw new ArgumentNullException("writer");
}
writer.packedFieldNumber = fieldNumber;
}
internal string SerializeType(Type type)
{
return TypeModel.SerializeType(this.model, type);
}
public void SetRootObject(object value)
{
this.NetCache.SetKeyedObject(0, value);
}
public static void WriteType(Type value, ProtoWriter writer)
{
bool flag = writer == null;
if (flag)
{
throw new ArgumentNullException("writer");
}
ProtoWriter.WriteString(writer.SerializeType(value), writer);
}
}
}
|