**** Merged from MCS ****
[mono-project.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap.Asn1 / Asn1Length.cs
blob0951ea907ea5a317875fea03e880e156b8fe3e52
1 /******************************************************************************
2 * The MIT License
3 * Copyright (c) 2003 Novell Inc. www.novell.com
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the Software), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *******************************************************************************/
24 // Novell.Directory.Ldap.Asn1.Asn1Length.cs
26 // Author:
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
32 using System;
34 namespace Novell.Directory.Ldap.Asn1
37 /// <summary> This class provides a means to manipulate ASN.1 Length's. It will
38 /// be used by Asn1Encoder's and Asn1Decoder's by composition.
39 /// </summary>
40 [CLSCompliantAttribute(true)]
41 public class Asn1Length
43 /// <summary> Returns the length of this Asn1Length.</summary>
44 virtual public int Length
46 get
48 return length;
52 /// <summary> Returns the encoded length of this Asn1Length.</summary>
53 virtual public int EncodedLength
55 get
57 return encodedLength;
62 /* Private variables
65 private int length;
66 private int encodedLength;
68 /* Constructors for Asn1Length
71 /// <summary> Constructs an empty Asn1Length. Values are added by calling reset</summary>
72 public Asn1Length()
75 /// <summary> Constructs an Asn1Length</summary>
76 public Asn1Length(int length)
78 this.length = length;
81 /// <summary> Constructs an Asn1Length object by decoding data from an
82 /// input stream.
83 ///
84 /// </summary>
85 /// <param name="in">A byte stream that contains the encoded ASN.1
86 ///
87 /// </param>
88 public Asn1Length(System.IO.Stream in_Renamed)
90 int r = in_Renamed.ReadByte();
91 encodedLength++;
92 if (r == 0x80)
93 length = - 1;
94 else if (r < 0x80)
95 length = r;
96 else
98 length = 0;
99 for (r = r & 0x7F; r > 0; r--)
101 int part = in_Renamed.ReadByte();
102 encodedLength++;
103 if (part < 0)
104 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Asn1Length");
105 length = (length << 8) + part;
110 /// <summary> Resets an Asn1Length object by decoding data from an
111 /// input stream.
112 ///
113 /// Note: this was added for optimization of Asn1.LBERdecoder.decode()
114 ///
115 /// </summary>
116 /// <param name="in">A byte stream that contains the encoded ASN.1
117 ///
118 /// </param>
119 public void reset(System.IO.Stream in_Renamed)
121 encodedLength = 0;
122 int r = in_Renamed.ReadByte();
123 encodedLength++;
124 if (r == 0x80)
125 length = - 1;
126 else if (r < 0x80)
127 length = r;
128 else
130 length = 0;
131 for (r = r & 0x7F; r > 0; r--)
133 int part = in_Renamed.ReadByte();
134 encodedLength++;
135 if (part < 0)
136 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Asn1Length");
137 length = (length << 8) + part;