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 /// An Asn1Identifier is composed of three parts:
40 /// <li> a class type,</li>
41 /// <li> a form, and</li>
44 /// The class type is defined as:
47 /// ------- -----------
53 /// The form is defined as:
61 /// Note: CONSTRUCTED types are made up of other CONSTRUCTED or PRIMITIVE
64 /// The tag is defined as:
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 public class Asn1Identifier:System.Object, System.ICloneable
77 /// <summary> Returns the CLASS of this Asn1Identifier as an int value.
80 /// <seealso cref="UNIVERSAL">
82 /// <seealso cref="APPLICATION">
84 /// <seealso cref="CONTEXT">
86 /// <seealso cref="PRIVATE">
88 virtual public int Asn1Class
96 /// <summary> Return a boolean indicating if the constructed bit is set.
99 /// <returns> true if constructed and false if primitive.
101 virtual public bool Constructed
109 /// <summary> Returns the TAG of this Asn1Identifier.</summary>
110 virtual public int Tag
118 /// <summary> Returns the encoded length of this Asn1Identifier.</summary>
119 virtual public int EncodedLength
123 return encodedLength;
127 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
128 /// has a TAG CLASS of UNIVERSAL.
131 /// <seealso cref="UNIVERSAL">
133 [CLSCompliantAttribute(false)]
134 virtual public bool Universal
138 return tagClass == UNIVERSAL;
142 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
143 /// has a TAG CLASS of APPLICATION.
146 /// <seealso cref="APPLICATION">
148 [CLSCompliantAttribute(false)]
149 virtual public bool Application
153 return tagClass == APPLICATION;
157 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
158 /// has a TAG CLASS of CONTEXT-SPECIFIC.
161 /// <seealso cref="CONTEXT">
163 [CLSCompliantAttribute(false)]
164 virtual public bool Context
168 return tagClass == CONTEXT;
172 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
173 /// has a TAG CLASS of PRIVATE.
176 /// <seealso cref="PRIVATE"></seealso>
177 [CLSCompliantAttribute(false)]
178 virtual public bool Private
182 return tagClass == PRIVATE;
187 /// <summary> Universal tag class.
191 public const int UNIVERSAL = 0;
193 /// <summary> Application-wide tag class.
197 public const int APPLICATION = 1;
199 /// <summary> Context-specific tag class.
203 public const int CONTEXT = 2;
205 /// <summary> Private-use tag class.
209 public const int PRIVATE = 3;
215 private int tagClass;
216 private bool constructed;
218 private int encodedLength;
220 /* Constructors for Asn1Identifier
223 /// <summary> Constructs an Asn1Identifier using the classtype, form and tag.
226 /// <param name="tagClass">As defined above.
229 /// <param name="constructed">Set to true if constructed and false if primitive.
232 /// <param name="tag">The tag of this identifier
234 public Asn1Identifier(int tagClass, bool constructed, int tag)
236 this.tagClass = tagClass;
237 this.constructed = constructed;
241 /// <summary> Decode an Asn1Identifier directly from an InputStream and
242 /// save the encoded length of the Asn1Identifier.
245 /// <param name="in">The input stream to decode from.
247 public Asn1Identifier(System.IO.Stream in_Renamed)
249 int r = in_Renamed.ReadByte();
252 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Identifier");
254 constructed = (r & 0x20) != 0;
255 tag = r & 0x1F; // if tag < 30 then its a single octet identifier.
257 // if true, its a multiple octet identifier.
258 tag = decodeTagNumber(in_Renamed);
262 public Asn1Identifier()
267 /// <summary> Decode an Asn1Identifier directly from an InputStream and
268 /// save the encoded length of the Asn1Identifier, but reuse the object.
271 /// <param name="in">The input stream to decode from.
273 public void reset(System.IO.Stream in_Renamed)
276 int r = in_Renamed.ReadByte();
279 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Identifier");
281 constructed = (r & 0x20) != 0;
282 tag = r & 0x1F; // if tag < 30 then its a single octet identifier.
284 // if true, its a multiple octet identifier.
285 tag = decodeTagNumber(in_Renamed);
288 /// <summary> In the case that we have a tag number that is greater than 30, we need
289 /// to decode a multiple octet tag number.
291 private int decodeTagNumber(System.IO.Stream in_Renamed)
296 int r = in_Renamed.ReadByte();
299 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in tag number");
300 n = (n << 7) + (r & 0x7F);
307 /* Convenience methods
310 /// <summary> Creates a duplicate, not a true clone, of this object and returns
311 /// a reference to the duplicate.
314 public System.Object Clone()
318 return base.MemberwiseClone();
320 catch (System.Exception ce)
322 throw new System.SystemException("Internal error, cannot create clone");