2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap.Asn1 / Asn1Length.cs
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:
11
12 * The above copyright notice and this permission notice shall be included in 
13 * all copies or substantial portions of the Software.
14
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 *******************************************************************************/
23 //
24 // Novell.Directory.Ldap.Asn1.Asn1Length.cs
25 //
26 // Author:
27 //   Sunil Kumar (Sunilk@novell.com)
28 //
29 // (C) 2003 Novell, Inc (http://www.novell.com)
30 //
31
32 using System;
33
34 namespace Novell.Directory.Ldap.Asn1
35 {
36         
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
42         {
43                 /// <summary> Returns the length of this Asn1Length.</summary>
44                 virtual public int Length
45                 {
46                         get
47                         {
48                                 return length;
49                         }
50                         
51                 }
52                 /// <summary> Returns the encoded length of this Asn1Length.</summary>
53                 virtual public int EncodedLength
54                 {
55                         get
56                         {
57                                 return encodedLength;
58                         }
59                         
60                 }
61                 
62                 /* Private variables
63                 */
64                 
65                 private int length;
66                 private int encodedLength;
67                 
68                 /* Constructors for Asn1Length
69                 */
70                 
71                 /// <summary> Constructs an empty Asn1Length.  Values are added by calling reset</summary>
72                 public Asn1Length()
73                 {
74                 }
75                 /// <summary> Constructs an Asn1Length</summary>
76                 public Asn1Length(int length)
77                 {
78                         this.length = length;
79                 }
80                 
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)
89                 {
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
97                         {
98                                 length = 0;
99                                 for (r = r & 0x7F; r > 0; r--)
100                                 {
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;
106                                 }
107                         }
108                 }
109                 
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)
120                 {
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
129                         {
130                                 length = 0;
131                                 for (r = r & 0x7F; r > 0; r--)
132                                 {
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;
138                                 }
139                         }
140                 }
141         }
142 }