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
76 [CLSCompliantAttribute(true)]
78 public class Asn1Identifier:System.Object, System.ICloneable
80 /// <summary> Returns the CLASS of this Asn1Identifier as an int value.
83 /// <seealso cref="UNIVERSAL">
85 /// <seealso cref="APPLICATION">
87 /// <seealso cref="CONTEXT">
89 /// <seealso cref="PRIVATE">
91 virtual public int Asn1Class
99 /// <summary> Return a boolean indicating if the constructed bit is set.
102 /// <returns> true if constructed and false if primitive.
104 virtual public bool Constructed
112 /// <summary> Returns the TAG of this Asn1Identifier.</summary>
113 virtual public int Tag
121 /// <summary> Returns the encoded length of this Asn1Identifier.</summary>
122 virtual public int EncodedLength
126 return encodedLength;
130 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
131 /// has a TAG CLASS of UNIVERSAL.
134 /// <seealso cref="UNIVERSAL">
136 [CLSCompliantAttribute(false)]
137 virtual public bool Universal
141 return tagClass == UNIVERSAL;
145 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
146 /// has a TAG CLASS of APPLICATION.
149 /// <seealso cref="APPLICATION">
151 [CLSCompliantAttribute(false)]
152 virtual public bool Application
156 return tagClass == APPLICATION;
160 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
161 /// has a TAG CLASS of CONTEXT-SPECIFIC.
164 /// <seealso cref="CONTEXT">
166 [CLSCompliantAttribute(false)]
167 virtual public bool Context
171 return tagClass == CONTEXT;
175 /// <summary> Returns a boolean value indicating whether or not this Asn1Identifier
176 /// has a TAG CLASS of PRIVATE.
179 /// <seealso cref="PRIVATE"></seealso>
180 [CLSCompliantAttribute(false)]
181 virtual public bool Private
185 return tagClass == PRIVATE;
190 /// <summary> Universal tag class.
194 public const int UNIVERSAL = 0;
196 /// <summary> Application-wide tag class.
200 public const int APPLICATION = 1;
202 /// <summary> Context-specific tag class.
206 public const int CONTEXT = 2;
208 /// <summary> Private-use tag class.
212 public const int PRIVATE = 3;
218 private int tagClass;
219 private bool constructed;
221 private int encodedLength;
223 /* Constructors for Asn1Identifier
226 /// <summary> Constructs an Asn1Identifier using the classtype, form and tag.
229 /// <param name="tagClass">As defined above.
232 /// <param name="constructed">Set to true if constructed and false if primitive.
235 /// <param name="tag">The tag of this identifier
237 public Asn1Identifier(int tagClass, bool constructed, int tag)
239 this.tagClass = tagClass;
240 this.constructed = constructed;
244 /// <summary> Decode an Asn1Identifier directly from an InputStream and
245 /// save the encoded length of the Asn1Identifier.
248 /// <param name="in">The input stream to decode from.
250 public Asn1Identifier(System.IO.Stream in_Renamed)
252 int r = in_Renamed.ReadByte();
255 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Identifier");
257 constructed = (r & 0x20) != 0;
258 tag = r & 0x1F; // if tag < 30 then its a single octet identifier.
260 // if true, its a multiple octet identifier.
261 tag = decodeTagNumber(in_Renamed);
265 public Asn1Identifier()
270 /// <summary> Decode an Asn1Identifier directly from an InputStream and
271 /// save the encoded length of the Asn1Identifier, but reuse the object.
274 /// <param name="in">The input stream to decode from.
276 public void reset(System.IO.Stream in_Renamed)
279 int r = in_Renamed.ReadByte();
282 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in Identifier");
284 constructed = (r & 0x20) != 0;
285 tag = r & 0x1F; // if tag < 30 then its a single octet identifier.
287 // if true, its a multiple octet identifier.
288 tag = decodeTagNumber(in_Renamed);
291 /// <summary> In the case that we have a tag number that is greater than 30, we need
292 /// to decode a multiple octet tag number.
294 private int decodeTagNumber(System.IO.Stream in_Renamed)
299 int r = in_Renamed.ReadByte();
302 throw new System.IO.EndOfStreamException("BERDecoder: decode: EOF in tag number");
303 n = (n << 7) + (r & 0x7F);
310 /* Convenience methods
313 /// <summary> Creates a duplicate, not a true clone, of this object and returns
314 /// a reference to the duplicate.
317 public System.Object Clone()
321 return base.MemberwiseClone();
323 catch (System.Exception ce)
325 throw new System.SystemException("Internal error, cannot create clone");