1 /******************************************************************************
3 * Copyright (c) 2003 Novell Inc. www.novell.com
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
22 *******************************************************************************/
24 // Novell.Directory.Ldap.Asn1.Asn1Identifier.cs
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
34 namespace Novell.Directory.Ldap.Asn1
37 /// <summary> This class is used to encapsulate an ASN.1 Identifier.
39 /// <p>An Asn1Identifier is composed of three parts:
40 /// <li> a class type,
44 /// <p>The class type is defined as:</p>
47 /// ------- -----------
53 /// <p> The form is defined as:</p>
61 /// <p> Note: CONSTRUCTED types are made up of other CONSTRUCTED or PRIMITIVE
64 /// <p> The tag is defined as:</p>
67 /// ------------- ---------------------------------------------
70 /// 1 1 1 1 0 (0-30) single octet tag
72 /// 1 1 1 1 1 (> 30) multiple octet tag, more octets follow
75 [CLSCompliantAttribute(true)]
76 public class Asn1Identifier:System.Object, System.ICloneable
78 /// <summary> Returns the CLASS of this Asn1Identifier as an int value.
81 /// <seealso cref="#UNIVERSAL">
83 /// <seealso cref="#APPLICATION">
85 /// <seealso cref="#CONTEXT">
87 /// <seealso cref="#PRIVATE">
89 virtual public int Asn1Class
97 /// <summary> Return a boolean indicating if the constructed bit is set.
100 /// <returns> true if constructed and false if primitive.
102 virtual public bool Constructed
110 /// <summary> Returns the TAG of this Asn1Identifier.</summary>
111 virtual public int Tag
119 /// <summary> Returns the encoded length of this Asn1Identifier.</summary>
120 virtual public int EncodedLength
124 return encodedLength;
128 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
129 /// has a TAG CLASS of UNIVERSAL.
132 /// <seealso cref="#UNIVERSAL">
134 [CLSCompliantAttribute(false)]
135 virtual public bool Universal
139 return tagClass == UNIVERSAL;
143 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
144 /// has a TAG CLASS of APPLICATION.
147 /// <seealso cref="#APPLICATION">
149 [CLSCompliantAttribute(false)]
150 virtual public bool Application
154 return tagClass == APPLICATION;
158 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
159 /// has a TAG CLASS of CONTEXT-SPECIFIC.
162 /// <seealso cref="#CONTEXT">
164 [CLSCompliantAttribute(false)]
165 virtual public bool Context
169 return tagClass == CONTEXT;
173 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
174 /// has a TAG CLASS of PRIVATE.
177 /// <seealso cref="#PRIVATE">
178 [CLSCompliantAttribute(false)]
179 virtual public bool Private
183 return tagClass == PRIVATE;
188 /// <summary> Universal tag class.
190 /// <p> UNIVERSAL = 0 </p>
192 public const int UNIVERSAL = 0;
194 /// <summary> Application-wide tag class.
196 /// <p> APPLICATION = 1 </p>
198 public const int APPLICATION = 1;
200 /// <summary> Context-specific tag class.
202 /// <p> CONTEXT = 2 </p>
204 public const int CONTEXT = 2;
206 /// <summary> Private-use tag class.
208 /// <p> PRIVATE = 3 </p>
210 public const int PRIVATE = 3;
216 private int tagClass;
217 private bool constructed;
219 private int encodedLength;
221 /* Constructors for Asn1Identifier
224 /// <summary> Constructs an Asn1Identifier using the classtype, form and tag.
227 /// <param name="tagClass">As defined above.
230 /// <param name="constructed">Set to true if constructed and false if primitive.
233 /// <param name="tag">The tag of this identifier
235 public Asn1Identifier(int tagClass, bool constructed, int tag)
237 this.tagClass = tagClass;
238 this.constructed = constructed;
242 /// <summary> Decode an Asn1Identifier directly from an InputStream and
243 /// save the encoded length of the Asn1Identifier.
246 /// <param name="in">The input stream to decode from.
248 public Asn1Identifier(System.IO.Stream in_Renamed)
250 int r = in_Renamed.ReadByte();
253 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Identifier");
255 constructed = (r & 0x20) != 0;
256 tag = r & 0x1F; // if tag < 30 then its a single octet identifier.
258 // if true, its a multiple octet identifier.
259 tag = decodeTagNumber(in_Renamed);
263 public Asn1Identifier()
268 /// <summary> Decode an Asn1Identifier directly from an InputStream and
269 /// save the encoded length of the Asn1Identifier, but reuse the object.
272 /// <param name="in">The input stream to decode from.
274 public void reset(System.IO.Stream in_Renamed)
277 int r = in_Renamed.ReadByte();
280 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Identifier");
282 constructed = (r & 0x20) != 0;
283 tag = r & 0x1F; // if tag < 30 then its a single octet identifier.
285 // if true, its a multiple octet identifier.
286 tag = decodeTagNumber(in_Renamed);
289 /// <summary> In the case that we have a tag number that is greater than 30, we need
290 /// to decode a multiple octet tag number.
292 private int decodeTagNumber(System.IO.Stream in_Renamed)
297 int r = in_Renamed.ReadByte();
300 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in tag number");
301 n = (n << 7) + (r & 0x7F);
308 /* Convenience methods
311 /// <summary> Creates a duplicate, not a true clone, of this object and returns
312 /// a reference to the duplicate.
315 public System.Object Clone()
319 return base.MemberwiseClone();
321 catch (System.Exception ce)
323 throw new System.SystemException("Internal error, cannot create clone");