File: XmlC14NWriter.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (1016 lines) | stat: -rw-r--r-- 45,683 bytes parent folder | download | duplicates (7)
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
namespace System.Xml
{
    using System.Collections;
    using System.IO;
    using System.Runtime;
    using System.Runtime.Serialization;
    using System.Text;

    sealed class XmlCanonicalWriter
    {
        XmlUTF8NodeWriter writer;
        MemoryStream elementStream;
        byte[] elementBuffer;
        XmlUTF8NodeWriter elementWriter;
        bool inStartElement;
        int depth;
        Scope[] scopes;
        int xmlnsAttributeCount;
        XmlnsAttribute[] xmlnsAttributes;
        int attributeCount;
        Attribute[] attributes;
        Attribute attribute;
        Element element;
        byte[] xmlnsBuffer;
        int xmlnsOffset;
        const int maxBytesPerChar = 3;
        int xmlnsStartOffset;
        bool includeComments;
        string[] inclusivePrefixes;
        const string xmlnsNamespace = "http://www.w3.org/2000/xmlns/";

        static readonly bool[] isEscapedAttributeChar = new bool[]
        {
            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, // All
            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
            false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, // '"', '&'
            false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false  // '<'
        };
        static readonly bool[] isEscapedElementChar = new bool[]
        {
            true, true, true, true, true, true, true, true, true, false, false, true, true, true, true, true, // All but 0x09, 0x0A
            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
            false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, // '&'
            false, false, false, false, false, false, false, false, false, false, false, false, true, false, true, false  // '<', '>'
        };

        public XmlCanonicalWriter()
        {
        }

        public void SetOutput(Stream stream, bool includeComments, string[] inclusivePrefixes)
        {
            if (stream == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("stream");

            if (writer == null)
            {
                writer = new XmlUTF8NodeWriter(isEscapedAttributeChar, isEscapedElementChar);
            }
            writer.SetOutput(stream, false, null);

            if (elementStream == null)
            {
                elementStream = new MemoryStream();
            }

            if (elementWriter == null)
            {
                elementWriter = new XmlUTF8NodeWriter(isEscapedAttributeChar, isEscapedElementChar);
            }
            elementWriter.SetOutput(elementStream, false, null);

            if (xmlnsAttributes == null)
            {
                xmlnsAttributeCount = 0;
                xmlnsOffset = 0;
                WriteXmlnsAttribute("xml", "http://www.w3.org/XML/1998/namespace");
                WriteXmlnsAttribute("xmlns", xmlnsNamespace);
                WriteXmlnsAttribute(string.Empty, string.Empty);
                xmlnsStartOffset = xmlnsOffset;
                for (int i = 0; i < 3; i++)
                {
                    xmlnsAttributes[i].referred = true;
                }
            }
            else
            {
                xmlnsAttributeCount = 3;
                xmlnsOffset = xmlnsStartOffset;
            }

            depth = 0;
            inStartElement = false;
            this.includeComments = includeComments;
            this.inclusivePrefixes = null;
            if (inclusivePrefixes != null)
            {
                this.inclusivePrefixes = new string[inclusivePrefixes.Length];
                for (int i = 0; i < inclusivePrefixes.Length; ++i)
                {
                    if (inclusivePrefixes[i] == null)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.InvalidInclusivePrefixListCollection));
                    }
                    this.inclusivePrefixes[i] = inclusivePrefixes[i];
                }
            }
        }

        public void Flush()
        {
            ThrowIfClosed();
            writer.Flush();
        }

        public void Close()
        {
            if (writer != null)
                writer.Close();
            if (elementWriter != null)
                elementWriter.Close();
            if (elementStream != null && elementStream.Length > 512)
                elementStream = null;
            elementBuffer = null;
            if (scopes != null && scopes.Length > 16)
                scopes = null;
            if (attributes != null && attributes.Length > 16)
                attributes = null;
            if (xmlnsBuffer != null && xmlnsBuffer.Length > 1024)
            {
                xmlnsAttributes = null;
                xmlnsBuffer = null;
            }
            inclusivePrefixes = null;
        }

        public void WriteDeclaration()
        {
        }

        public void WriteComment(string value)
        {
            if (value == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
            ThrowIfClosed();
            if (includeComments)
            {
                writer.WriteComment(value);
            }
        }


        void StartElement()
        {
            if (scopes == null)
            {
                scopes = new Scope[4];
            }
            else if (depth == scopes.Length)
            {
                Scope[] newScopes = new Scope[depth * 2];
                Array.Copy(scopes, newScopes, depth);
                scopes = newScopes;
            }
            scopes[depth].xmlnsAttributeCount = xmlnsAttributeCount;
            scopes[depth].xmlnsOffset = xmlnsOffset;
            depth++;
            inStartElement = true;
            attributeCount = 0;
            elementStream.Position = 0;
        }

        void EndElement()
        {
            depth--;
            xmlnsAttributeCount = scopes[depth].xmlnsAttributeCount;
            xmlnsOffset = scopes[depth].xmlnsOffset;
        }

        public void WriteStartElement(string prefix, string localName)
        {
            if (prefix == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("prefix");
            if (localName == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localName");
            ThrowIfClosed();
            bool isRootElement = (this.depth == 0);

            StartElement();
            element.prefixOffset = elementWriter.Position + 1;
            element.prefixLength = Encoding.UTF8.GetByteCount(prefix);
            element.localNameOffset = element.prefixOffset + element.prefixLength + (element.prefixLength != 0 ? 1 : 0);
            element.localNameLength = Encoding.UTF8.GetByteCount(localName);
            elementWriter.WriteStartElement(prefix, localName);

            // If we have a inclusivenamespace prefix list and the namespace declaration is in the 
            // outer context, then Add it to the root element.
            if (isRootElement && (this.inclusivePrefixes != null))
            {
                // Scan through all the namespace declarations in the outer scope.
                for (int i = 0; i < this.scopes[0].xmlnsAttributeCount; ++i)
                {
                    if (IsInclusivePrefix(ref xmlnsAttributes[i]))
                    {
                        XmlnsAttribute attribute = xmlnsAttributes[i];
                        AddXmlnsAttribute(ref attribute);
                    }
                }
            }
        }

        public void WriteStartElement(byte[] prefixBuffer, int prefixOffset, int prefixLength, byte[] localNameBuffer, int localNameOffset, int localNameLength)
        {
            if (prefixBuffer == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("prefixBuffer"));
            if (prefixOffset < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixOffset", SR.GetString(SR.ValueMustBeNonNegative)));
            if (prefixOffset > prefixBuffer.Length)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixOffset", SR.GetString(SR.OffsetExceedsBufferSize, prefixBuffer.Length)));
            if (prefixLength < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixLength", SR.GetString(SR.ValueMustBeNonNegative)));
            if (prefixLength > prefixBuffer.Length - prefixOffset)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixLength", SR.GetString(SR.SizeExceedsRemainingBufferSpace, prefixBuffer.Length - prefixOffset)));

            if (localNameBuffer == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("localNameBuffer"));
            if (localNameOffset < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("localNameOffset", SR.GetString(SR.ValueMustBeNonNegative)));
            if (localNameOffset > localNameBuffer.Length)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("localNameOffset", SR.GetString(SR.OffsetExceedsBufferSize, localNameBuffer.Length)));
            if (localNameLength < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("localNameLength", SR.GetString(SR.ValueMustBeNonNegative)));
            if (localNameLength > localNameBuffer.Length - localNameOffset)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("localNameLength", SR.GetString(SR.SizeExceedsRemainingBufferSpace, localNameBuffer.Length - localNameOffset)));
            ThrowIfClosed();
            bool isRootElement = (this.depth == 0);

            StartElement();
            element.prefixOffset = elementWriter.Position + 1;
            element.prefixLength = prefixLength;
            element.localNameOffset = element.prefixOffset + prefixLength + (prefixLength != 0 ? 1 : 0);
            element.localNameLength = localNameLength;
            elementWriter.WriteStartElement(prefixBuffer, prefixOffset, prefixLength, localNameBuffer, localNameOffset, localNameLength);

            // If we have a inclusivenamespace prefix list and the namespace declaration is in the 
            // outer context, then Add it to the root element.
            if (isRootElement && (this.inclusivePrefixes != null))
            {
                // Scan through all the namespace declarations in the outer scope.
                for (int i = 0; i < this.scopes[0].xmlnsAttributeCount; ++i)
                {
                    if (IsInclusivePrefix(ref xmlnsAttributes[i]))
                    {
                        XmlnsAttribute attribute = xmlnsAttributes[i];
                        AddXmlnsAttribute(ref attribute);
                    }
                }
            }

        }

        bool IsInclusivePrefix(ref XmlnsAttribute xmlnsAttribute)
        {
            for (int i = 0; i < this.inclusivePrefixes.Length; ++i)
            {
                if (this.inclusivePrefixes[i].Length == xmlnsAttribute.prefixLength)
                {
                    if (String.Compare(Encoding.UTF8.GetString(xmlnsBuffer, xmlnsAttribute.prefixOffset, xmlnsAttribute.prefixLength), this.inclusivePrefixes[i], StringComparison.Ordinal) == 0)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public void WriteEndStartElement(bool isEmpty)
        {
            ThrowIfClosed();
            elementWriter.Flush();
            elementBuffer = elementStream.GetBuffer();
            inStartElement = false;
            ResolvePrefixes();
            writer.WriteStartElement(elementBuffer, element.prefixOffset, element.prefixLength, elementBuffer, element.localNameOffset, element.localNameLength);
            for (int i = scopes[depth - 1].xmlnsAttributeCount; i < xmlnsAttributeCount; i++)
            {
                // Check if this prefix with the same namespace has already been rendered.
                int j = i - 1;
                bool alreadyReferred = false;
                while (j >= 0)
                {
                    if (Equals(xmlnsBuffer, xmlnsAttributes[i].prefixOffset, xmlnsAttributes[i].prefixLength, xmlnsBuffer, xmlnsAttributes[j].prefixOffset, xmlnsAttributes[j].prefixLength))
                    {
                        // Check if the namespace is also equal.
                        if (Equals(xmlnsBuffer, xmlnsAttributes[i].nsOffset, xmlnsAttributes[i].nsLength, xmlnsBuffer, xmlnsAttributes[j].nsOffset, xmlnsAttributes[j].nsLength))
                        {
                            // We have found the prefix with the same namespace occur before. See if this has been
                            // referred.
                            if (xmlnsAttributes[j].referred)
                            {
                                // This has been referred previously. So we don't have 
                                // to output the namespace again.
                                alreadyReferred = true;
                                break;
                            }
                        }
                        else
                        {
                            // The prefix is the same, but the namespace value has changed. So we have to 
                            // output this namespace.
                            break;
                        }
                    }
                    --j;
                }

                if (!alreadyReferred)
                {
                    WriteXmlnsAttribute(ref xmlnsAttributes[i]);
                }
            }
            if (attributeCount > 0)
            {
                if (attributeCount > 1)
                {
                    SortAttributes();
                }

                for (int i = 0; i < attributeCount; i++)
                {
                    writer.WriteText(elementBuffer, attributes[i].offset, attributes[i].length);
                }
            }
            writer.WriteEndStartElement(false);
            if (isEmpty)
            {
                writer.WriteEndElement(elementBuffer, element.prefixOffset, element.prefixLength, elementBuffer, element.localNameOffset, element.localNameLength);
                EndElement();
            }
            elementBuffer = null;
        }

        public void WriteEndElement(string prefix, string localName)
        {
            if (prefix == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("prefix");
            if (localName == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localName");
            ThrowIfClosed();
            writer.WriteEndElement(prefix, localName);
            EndElement();
        }

        void EnsureXmlnsBuffer(int byteCount)
        {
            if (xmlnsBuffer == null)
            {
                xmlnsBuffer = new byte[Math.Max(byteCount, 128)];
            }
            else if (xmlnsOffset + byteCount > xmlnsBuffer.Length)
            {
                byte[] newBuffer = new byte[Math.Max(xmlnsOffset + byteCount, xmlnsBuffer.Length * 2)];
                Buffer.BlockCopy(xmlnsBuffer, 0, newBuffer, 0, xmlnsOffset);
                xmlnsBuffer = newBuffer;
            }
        }

        public void WriteXmlnsAttribute(string prefix, string ns)
        {
            if (prefix == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("prefix");
            if (ns == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ns");
            ThrowIfClosed();
            if (prefix.Length > int.MaxValue - ns.Length)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("ns", SR.GetString(SR.CombinedPrefixNSLength, int.MaxValue / maxBytesPerChar)));
            int totalLength = prefix.Length + ns.Length;
            if (totalLength > int.MaxValue / maxBytesPerChar)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("ns", SR.GetString(SR.CombinedPrefixNSLength, int.MaxValue / maxBytesPerChar)));
            EnsureXmlnsBuffer(totalLength * maxBytesPerChar);
            XmlnsAttribute xmlnsAttribute;
            xmlnsAttribute.prefixOffset = xmlnsOffset;
            xmlnsAttribute.prefixLength = Encoding.UTF8.GetBytes(prefix, 0, prefix.Length, xmlnsBuffer, xmlnsOffset);
            xmlnsOffset += xmlnsAttribute.prefixLength;
            xmlnsAttribute.nsOffset = xmlnsOffset;
            xmlnsAttribute.nsLength = Encoding.UTF8.GetBytes(ns, 0, ns.Length, xmlnsBuffer, xmlnsOffset);
            xmlnsOffset += xmlnsAttribute.nsLength;
            xmlnsAttribute.referred = false;
            AddXmlnsAttribute(ref xmlnsAttribute);
        }

        public void WriteXmlnsAttribute(byte[] prefixBuffer, int prefixOffset, int prefixLength, byte[] nsBuffer, int nsOffset, int nsLength)
        {
            if (prefixBuffer == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("prefixBuffer"));
            if (prefixOffset < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixOffset", SR.GetString(SR.ValueMustBeNonNegative)));
            if (prefixOffset > prefixBuffer.Length)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixOffset", SR.GetString(SR.OffsetExceedsBufferSize, prefixBuffer.Length)));
            if (prefixLength < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixLength", SR.GetString(SR.ValueMustBeNonNegative)));
            if (prefixLength > prefixBuffer.Length - prefixOffset)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixLength", SR.GetString(SR.SizeExceedsRemainingBufferSpace, prefixBuffer.Length - prefixOffset)));

            if (nsBuffer == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("nsBuffer"));
            if (nsOffset < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("nsOffset", SR.GetString(SR.ValueMustBeNonNegative)));
            if (nsOffset > nsBuffer.Length)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("nsOffset", SR.GetString(SR.OffsetExceedsBufferSize, nsBuffer.Length)));
            if (nsLength < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("nsLength", SR.GetString(SR.ValueMustBeNonNegative)));
            if (nsLength > nsBuffer.Length - nsOffset)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("nsLength", SR.GetString(SR.SizeExceedsRemainingBufferSpace, nsBuffer.Length - nsOffset)));
            ThrowIfClosed();
            if (prefixLength > int.MaxValue - nsLength)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("nsLength", SR.GetString(SR.CombinedPrefixNSLength, int.MaxValue)));
            EnsureXmlnsBuffer(prefixLength + nsLength);
            XmlnsAttribute xmlnsAttribute;
            xmlnsAttribute.prefixOffset = xmlnsOffset;
            xmlnsAttribute.prefixLength = prefixLength;
            Buffer.BlockCopy(prefixBuffer, prefixOffset, xmlnsBuffer, xmlnsOffset, prefixLength);
            xmlnsOffset += prefixLength;
            xmlnsAttribute.nsOffset = xmlnsOffset;
            xmlnsAttribute.nsLength = nsLength;
            Buffer.BlockCopy(nsBuffer, nsOffset, xmlnsBuffer, xmlnsOffset, nsLength);
            xmlnsOffset += nsLength;
            xmlnsAttribute.referred = false;
            AddXmlnsAttribute(ref xmlnsAttribute);
        }

        public void WriteStartAttribute(string prefix, string localName)
        {
            if (prefix == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("prefix");
            if (localName == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localName");
            ThrowIfClosed();
            attribute.offset = elementWriter.Position;
            attribute.length = 0;
            attribute.prefixOffset = attribute.offset + 1; // WriteStartAttribute emits a space
            attribute.prefixLength = Encoding.UTF8.GetByteCount(prefix);
            attribute.localNameOffset = attribute.prefixOffset + attribute.prefixLength + (attribute.prefixLength != 0 ? 1 : 0);
            attribute.localNameLength = Encoding.UTF8.GetByteCount(localName);
            attribute.nsOffset = 0;
            attribute.nsLength = 0;
            elementWriter.WriteStartAttribute(prefix, localName);
        }

        public void WriteStartAttribute(byte[] prefixBuffer, int prefixOffset, int prefixLength, byte[] localNameBuffer, int localNameOffset, int localNameLength)
        {
            if (prefixBuffer == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("prefixBuffer"));
            if (prefixOffset < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixOffset", SR.GetString(SR.ValueMustBeNonNegative)));
            if (prefixOffset > prefixBuffer.Length)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixOffset", SR.GetString(SR.OffsetExceedsBufferSize, prefixBuffer.Length)));
            if (prefixLength < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixLength", SR.GetString(SR.ValueMustBeNonNegative)));
            if (prefixLength > prefixBuffer.Length - prefixOffset)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("prefixLength", SR.GetString(SR.SizeExceedsRemainingBufferSpace, prefixBuffer.Length - prefixOffset)));

            if (localNameBuffer == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("localNameBuffer"));
            if (localNameOffset < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("localNameOffset", SR.GetString(SR.ValueMustBeNonNegative)));
            if (localNameOffset > localNameBuffer.Length)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("localNameOffset", SR.GetString(SR.OffsetExceedsBufferSize, localNameBuffer.Length)));
            if (localNameLength < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("localNameLength", SR.GetString(SR.ValueMustBeNonNegative)));
            if (localNameLength > localNameBuffer.Length - localNameOffset)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("localNameLength", SR.GetString(SR.SizeExceedsRemainingBufferSpace, localNameBuffer.Length - localNameOffset)));
            ThrowIfClosed();
            attribute.offset = elementWriter.Position;
            attribute.length = 0;
            attribute.prefixOffset = attribute.offset + 1; // WriteStartAttribute emits a space
            attribute.prefixLength = prefixLength;
            attribute.localNameOffset = attribute.prefixOffset + prefixLength + (prefixLength != 0 ? 1 : 0);
            attribute.localNameLength = localNameLength;
            attribute.nsOffset = 0;
            attribute.nsLength = 0;
            elementWriter.WriteStartAttribute(prefixBuffer, prefixOffset, prefixLength, localNameBuffer, localNameOffset, localNameLength);
        }

        public void WriteEndAttribute()
        {
            ThrowIfClosed();
            elementWriter.WriteEndAttribute();
            attribute.length = elementWriter.Position - attribute.offset;
            AddAttribute(ref attribute);
        }

        public void WriteCharEntity(int ch)
        {
            ThrowIfClosed();
            if (ch <= char.MaxValue)
            {
                char[] chars = new char[1] { (char)ch };
                WriteEscapedText(chars, 0, 1);
            }
            else
            {
                WriteText(ch);
            }
        }

        public void WriteEscapedText(string value)
        {
            if (value == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
            ThrowIfClosed();
            // Skip all white spaces before the start of root element.
            if (this.depth > 0)
            {
                if (inStartElement)
                {
                    elementWriter.WriteEscapedText(value);
                }
                else
                {
                    writer.WriteEscapedText(value);
                }
            }
        }

        public void WriteEscapedText(byte[] chars, int offset, int count)
        {
            if (chars == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("chars"));
            if (offset < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
            if (offset > chars.Length)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.OffsetExceedsBufferSize, chars.Length)));
            if (count < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
            if (count > chars.Length - offset)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.SizeExceedsRemainingBufferSpace, chars.Length - offset)));
            ThrowIfClosed();
            // Skip all white spaces before the start of root element.
            if (this.depth > 0)
            {
                if (inStartElement)
                {
                    elementWriter.WriteEscapedText(chars, offset, count);
                }
                else
                {
                    writer.WriteEscapedText(chars, offset, count);
                }
            }
        }

        public void WriteEscapedText(char[] chars, int offset, int count)
        {
            ThrowIfClosed();
            // Skip all white spaces before the start of root element.
            if (this.depth > 0)
            {
                if (inStartElement)
                {
                    elementWriter.WriteEscapedText(chars, offset, count);
                }
                else
                {
                    writer.WriteEscapedText(chars, offset, count);
                }
            }
        }

#if OLDWRITER
        unsafe internal void WriteText(char* chars, int charCount)
        {
            ThrowIfClosed();
            if (inStartElement)
            {
                elementWriter.WriteText(chars, charCount);
            }
            else
            {
                writer.WriteText(chars, charCount);
            }
        }
        unsafe internal void WriteEscapedText(char* chars, int count)
        {
            ThrowIfClosed();
            // Skip all white spaces before the start of root element.
            if (this.depth > 0)
            {
                if (inStartElement)
                {
                    elementWriter.WriteEscapedText(chars, count);
                }
                else
                {
                    writer.WriteEscapedText(chars, count);
                }
            }
        }
#endif

        public void WriteText(int ch)
        {
            ThrowIfClosed();
            if (inStartElement)
            {
                elementWriter.WriteText(ch);
            }
            else
            {
                writer.WriteText(ch);
            }
        }

        public void WriteText(byte[] chars, int offset, int count)
        {
            ThrowIfClosed();
            if (chars == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("chars"));
            if (offset < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
            if (offset > chars.Length)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.OffsetExceedsBufferSize, chars.Length)));
            if (count < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
            if (count > chars.Length - offset)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.SizeExceedsRemainingBufferSpace, chars.Length - offset)));
            if (inStartElement)
            {
                elementWriter.WriteText(chars, offset, count);
            }
            else
            {
                writer.WriteText(chars, offset, count);
            }
        }

        public void WriteText(string value)
        {
            if (value == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
            if (value.Length > 0)
            {
                if (inStartElement)
                {
                    elementWriter.WriteText(value);
                }
                else
                {
                    writer.WriteText(value);
                }
            }
        }

        public void WriteText(char[] chars, int offset, int count)
        {
            ThrowIfClosed();
            if (chars == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("chars"));
            if (offset < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
            if (offset > chars.Length)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.OffsetExceedsBufferSize, chars.Length)));
            if (count < 0)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
            if (count > chars.Length - offset)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.SizeExceedsRemainingBufferSpace, chars.Length - offset)));
            if (inStartElement)
            {
                elementWriter.WriteText(chars, offset, count);
            }
            else
            {
                writer.WriteText(chars, offset, count);
            }
        }

        void ThrowIfClosed()
        {
            if (writer == null)
                ThrowClosed();
        }

        void ThrowClosed()
        {
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().ToString()));
        }

        void WriteXmlnsAttribute(ref XmlnsAttribute xmlnsAttribute)
        {
            if (xmlnsAttribute.referred)
            {
                writer.WriteXmlnsAttribute(xmlnsBuffer, xmlnsAttribute.prefixOffset, xmlnsAttribute.prefixLength, xmlnsBuffer, xmlnsAttribute.nsOffset, xmlnsAttribute.nsLength);
            }
        }

        void SortAttributes()
        {
            if (attributeCount < 16)
            {
                for (int i = 0; i < attributeCount - 1; i++)
                {
                    int attributeMin = i;
                    for (int j = i + 1; j < attributeCount; j++)
                    {
                        if (Compare(ref attributes[j], ref attributes[attributeMin]) < 0)
                        {
                            attributeMin = j;
                        }
                    }

                    if (attributeMin != i)
                    {
                        Attribute temp = attributes[i];
                        attributes[i] = attributes[attributeMin];
                        attributes[attributeMin] = temp;
                    }
                }
            }
            else
            {
                new AttributeSorter(this).Sort();
            }
        }

        void AddAttribute(ref Attribute attribute)
        {
            if (attributes == null)
            {
                attributes = new Attribute[4];
            }
            else if (attributeCount == attributes.Length)
            {
                Attribute[] newAttributes = new Attribute[attributeCount * 2];
                Array.Copy(attributes, newAttributes, attributeCount);
                attributes = newAttributes;
            }

            attributes[attributeCount] = attribute;
            attributeCount++;
        }

        void AddXmlnsAttribute(ref XmlnsAttribute xmlnsAttribute)
        {
            //            Console.WriteLine("{0}={1}", Encoding.UTF8.GetString(xmlnsBuffer, xmlnsAttribute.prefixOffset, xmlnsAttribute.prefixLength), 
            //                                Encoding.UTF8.GetString(xmlnsBuffer, xmlnsAttribute.nsOffset, xmlnsAttribute.nsLength));

            if (xmlnsAttributes == null)
            {
                xmlnsAttributes = new XmlnsAttribute[4];
            }
            else if (xmlnsAttributes.Length == xmlnsAttributeCount)
            {
                XmlnsAttribute[] newXmlnsAttributes = new XmlnsAttribute[xmlnsAttributeCount * 2];
                Array.Copy(xmlnsAttributes, newXmlnsAttributes, xmlnsAttributeCount);
                xmlnsAttributes = newXmlnsAttributes;
            }

            // If the prefix is in the inclusive prefix list, then mark it as 
            // to be rendered. Depth 0 is outer context and those can be ignored
            // for now.
            if ((depth > 0) && (this.inclusivePrefixes != null))
            {
                if (IsInclusivePrefix(ref xmlnsAttribute))
                {
                    xmlnsAttribute.referred = true;
                }
            }

            if (depth == 0)
            {
                // XmlnsAttributes at depth 0 are the outer context.  They don't need to be sorted.
                xmlnsAttributes[xmlnsAttributeCount++] = xmlnsAttribute;
            }
            else
            {
                // Sort the xmlns xmlnsAttribute
                int xmlnsAttributeIndex = scopes[depth - 1].xmlnsAttributeCount;
                bool isNewPrefix = true;
                while (xmlnsAttributeIndex < xmlnsAttributeCount)
                {
                    int result = Compare(ref xmlnsAttribute, ref xmlnsAttributes[xmlnsAttributeIndex]);
                    if (result > 0)
                    {
                        xmlnsAttributeIndex++;
                    }
                    else if (result == 0)
                    {
                        // We already have the same prefix at this scope. So let's
                        // just replace the old one with the new.
                        xmlnsAttributes[xmlnsAttributeIndex] = xmlnsAttribute;
                        isNewPrefix = false;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }

                if (isNewPrefix)
                {
                    Array.Copy(xmlnsAttributes, xmlnsAttributeIndex, xmlnsAttributes, xmlnsAttributeIndex + 1, xmlnsAttributeCount - xmlnsAttributeIndex);
                    xmlnsAttributes[xmlnsAttributeIndex] = xmlnsAttribute;
                    xmlnsAttributeCount++;
                }
            }
        }

        void ResolvePrefix(int prefixOffset, int prefixLength, out int nsOffset, out int nsLength)
        {
            int xmlnsAttributeMin = scopes[depth - 1].xmlnsAttributeCount;

            // Lookup the attribute; it has to be there.  The decls are in sorted order
            // so we could do a binary search.
            int j = xmlnsAttributeCount - 1;
            while (!Equals(elementBuffer, prefixOffset, prefixLength,
                           xmlnsBuffer, xmlnsAttributes[j].prefixOffset, xmlnsAttributes[j].prefixLength))
            {
                j--;
            }

            nsOffset = xmlnsAttributes[j].nsOffset;
            nsLength = xmlnsAttributes[j].nsLength;

            if (j < xmlnsAttributeMin)
            {
                // If the xmlns decl isn't at this scope, see if we need to copy it down
                if (!xmlnsAttributes[j].referred)
                {
                    XmlnsAttribute xmlnsAttribute = xmlnsAttributes[j];
                    xmlnsAttribute.referred = true;

                    // This inserts the xmlns attribute in sorted order, so j is no longer valid
                    AddXmlnsAttribute(ref xmlnsAttribute);
                }
            }
            else
            {
                // Found at this scope, indicate we need to emit it
                xmlnsAttributes[j].referred = true;
            }
        }

        void ResolvePrefix(ref Attribute attribute)
        {
            if (attribute.prefixLength != 0)
            {
                ResolvePrefix(attribute.prefixOffset, attribute.prefixLength, out attribute.nsOffset, out attribute.nsLength);
            }
            else
            {
                // These should've been set when we added the prefix
                Fx.Assert(attribute.nsOffset == 0 && attribute.nsLength == 0, "");
            }
        }

        void ResolvePrefixes()
        {
            int nsOffset;
            int nsLength;
            ResolvePrefix(element.prefixOffset, element.prefixLength, out nsOffset, out nsLength);

            for (int i = 0; i < attributeCount; i++)
            {
                ResolvePrefix(ref attributes[i]);
            }
        }

        int Compare(ref XmlnsAttribute xmlnsAttribute1, ref XmlnsAttribute xmlnsAttribute2)
        {
            return Compare(xmlnsBuffer,
                           xmlnsAttribute1.prefixOffset, xmlnsAttribute1.prefixLength,
                           xmlnsAttribute2.prefixOffset, xmlnsAttribute2.prefixLength);
        }

        int Compare(ref Attribute attribute1, ref Attribute attribute2)
        {
            int s = Compare(xmlnsBuffer,
                            attribute1.nsOffset, attribute1.nsLength,
                            attribute2.nsOffset, attribute2.nsLength);

            if (s == 0)
            {
                s = Compare(elementBuffer,
                            attribute1.localNameOffset, attribute1.localNameLength,
                            attribute2.localNameOffset, attribute2.localNameLength);
            }

            return s;
        }

        int Compare(byte[] buffer, int offset1, int length1, int offset2, int length2)
        {
            if (offset1 == offset2)
            {
                return length1 - length2;
            }

            return Compare(buffer, offset1, length1, buffer, offset2, length2);
        }

        int Compare(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2)
        {
            //            Console.WriteLine("Compare: \"{0}\", \"{1}\"", Encoding.UTF8.GetString(sourceBuffer, offset1, length1), Encoding.UTF8.GetString(sourceBuffer, offset2, length2));

            int length = Math.Min(length1, length2);

            int s = 0;
            for (int i = 0; i < length && s == 0; i++)
            {
                s = buffer1[offset1 + i] - buffer2[offset2 + i];
            }

            if (s == 0)
            {
                s = length1 - length2;
            }

            return s;
        }

        bool Equals(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2)
        {
            //            Console.WriteLine("Equals: \"{0}\", \"{1}\"", Encoding.UTF8.GetString(buffer1, offset1, length1), Encoding.UTF8.GetString(buffer2, offset2, length2));

            if (length1 != length2)
                return false;

            for (int i = 0; i < length1; i++)
            {
                if (buffer1[offset1 + i] != buffer2[offset2 + i])
                {
                    return false;
                }
            }

            return true;
        }

        class AttributeSorter : IComparer
        {
            XmlCanonicalWriter writer;

            public AttributeSorter(XmlCanonicalWriter writer)
            {
                this.writer = writer;
            }

            public void Sort()
            {
                object[] indeces = new object[writer.attributeCount];

                for (int i = 0; i < indeces.Length; i++)
                {
                    indeces[i] = i;
                }

                Array.Sort(indeces, this);

                Attribute[] attributes = new Attribute[writer.attributes.Length];
                for (int i = 0; i < indeces.Length; i++)
                {
                    attributes[i] = writer.attributes[(int)indeces[i]];
                }

                writer.attributes = attributes;
            }

            public int Compare(object obj1, object obj2)
            {
                int attributeIndex1 = (int)obj1;
                int attributeIndex2 = (int)obj2;
                return writer.Compare(ref writer.attributes[attributeIndex1], ref writer.attributes[attributeIndex2]);
            }
        }

        struct Scope
        {
            public int xmlnsAttributeCount;
            public int xmlnsOffset;
        }

        struct Element
        {
            public int prefixOffset;
            public int prefixLength;
            public int localNameOffset;
            public int localNameLength;
        }

        struct Attribute
        {
            public int prefixOffset;
            public int prefixLength;
            public int localNameOffset;
            public int localNameLength;
            public int nsOffset;
            public int nsLength;
            public int offset;
            public int length;
        }

        struct XmlnsAttribute
        {
            public int prefixOffset;
            public int prefixLength;
            public int nsOffset;
            public int nsLength;
            public bool referred;
        }
    }
}