New test.
[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         public class Asn1Length
41         {
42                 /// <summary> Returns the length of this Asn1Length.</summary>
43                 virtual public int Length
44                 {
45                         get
46                         {
47                                 return length;
48                         }
49                         
50                 }
51                 /// <summary> Returns the encoded length of this Asn1Length.</summary>
52                 virtual public int EncodedLength
53                 {
54                         get
55                         {
56                                 return encodedLength;
57                         }
58                         
59                 }
60                 
61                 /* Private variables
62                 */
63                 
64                 private int length;
65                 private int encodedLength;
66                 
67                 /* Constructors for Asn1Length
68                 */
69                 
70                 /// <summary> Constructs an empty Asn1Length.  Values are added by calling reset</summary>
71                 public Asn1Length()
72                 {
73                 }
74                 /// <summary> Constructs an Asn1Length</summary>
75                 public Asn1Length(int length)
76                 {
77                         this.length = length;
78                 }
79                 
80                 /// <summary> Constructs an Asn1Length object by decoding data from an
81                 /// input stream.
82                 /// 
83                 /// </summary>
84                 /// <param name="in">A byte stream that contains the encoded ASN.1
85                 /// 
86                 /// </param>
87                 public Asn1Length(System.IO.Stream in_Renamed)
88                 {
89                         int r = in_Renamed.ReadByte();
90                         encodedLength++;
91                         if (r == 0x80)
92                                 length = - 1;
93                         else if (r < 0x80)
94                                 length = r;
95                         else
96                         {
97                                 length = 0;
98                                 for (r = r & 0x7F; r > 0; r--)
99                                 {
100                                         int part = in_Renamed.ReadByte();
101                                         encodedLength++;
102                                         if (part < 0)
103                                                 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Asn1Length");
104                                         length = (length << 8) + part;
105                                 }
106                         }
107                 }
108                 
109                 /// <summary> Resets an Asn1Length object by decoding data from an
110                 /// input stream.
111                 /// 
112                 /// Note: this was added for optimization of Asn1.LBERdecoder.decode()
113                 /// 
114                 /// </summary>
115                 /// <param name="in">A byte stream that contains the encoded ASN.1
116                 /// 
117                 /// </param>
118                 public void  reset(System.IO.Stream in_Renamed)
119                 {
120                         encodedLength = 0;
121                         int r = in_Renamed.ReadByte();
122                         encodedLength++;
123                         if (r == 0x80)
124                                 length = - 1;
125                         else if (r < 0x80)
126                                 length = r;
127                         else
128                         {
129                                 length = 0;
130                                 for (r = r & 0x7F; r > 0; r--)
131                                 {
132                                         int part = in_Renamed.ReadByte();
133                                         encodedLength++;
134                                         if (part < 0)
135                                                 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Asn1Length");
136                                         length = (length << 8) + part;
137                                 }
138                         }
139                 }
140         }
141 }