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.Asn1Structured.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 serves as the base type for all ASN.1 38 /// structured types. 39 /// </summary> 40 public abstract class Asn1Structured:Asn1Object 41 { 42 private Asn1Object[] content; 43 44 private int contentIndex = 0; 45 46 /* 47 * Create a an Asn1 structured type with default size of 10 48 * 49 * @param the Asn1Identifier containing the tag for this structured type 50 */ Asn1Structured(Asn1Identifier id)51 protected internal Asn1Structured(Asn1Identifier id):this(id, 10) 52 { 53 return ; 54 } 55 56 /* 57 * Create a an Asn1 structured type with the designated size 58 * 59 * @param id the Asn1Identifier containing the tag for this structured type 60 * 61 * @param size the size to allocate 62 */ Asn1Structured(Asn1Identifier id, int size)63 protected internal Asn1Structured(Asn1Identifier id, int size):base(id) 64 { 65 content = new Asn1Object[size]; 66 return ; 67 } 68 69 /* 70 * Create a an Asn1 structured type with default size of 10 71 * 72 * @param id the Asn1Identifier containing the tag for this structured type 73 * 74 * @param content an array containing the content 75 * 76 * @param size the number of items of content in the array 77 */ Asn1Structured(Asn1Identifier id, Asn1Object[] newContent, int size)78 protected internal Asn1Structured(Asn1Identifier id, Asn1Object[] newContent, int size):base(id) 79 { 80 content = newContent; 81 contentIndex = size; 82 return ; 83 } 84 85 /// <summary> Encodes the contents of this Asn1Structured directly to an output 86 /// stream. 87 /// </summary> encode(Asn1Encoder enc, System.IO.Stream out_Renamed)88 public override void encode(Asn1Encoder enc, System.IO.Stream out_Renamed) 89 { 90 enc.encode(this, out_Renamed); 91 return ; 92 } 93 94 /// <summary> Decode an Asn1Structured type from an InputStream.</summary> 95 [CLSCompliantAttribute(false)] decodeStructured(Asn1Decoder dec, System.IO.Stream in_Renamed, int len)96 protected internal void decodeStructured(Asn1Decoder dec, System.IO.Stream in_Renamed, int len) 97 { 98 int[] componentLen = new int[1]; // collects length of component 99 100 while (len > 0) 101 { 102 add(dec.decode(in_Renamed, componentLen)); 103 len -= componentLen[0]; 104 } 105 return ; 106 } 107 108 /// <summary> Returns an array containing the individual ASN.1 elements 109 /// of this Asn1Structed object. 110 /// 111 /// </summary> 112 /// <returns> an array of Asn1Objects 113 /// </returns> toArray()114 public Asn1Object[] toArray() 115 { 116 Asn1Object[] cloneArray = new Asn1Object[contentIndex]; 117 Array.Copy((System.Array) content, 0, (System.Array) cloneArray, 0, contentIndex); 118 return cloneArray; 119 } 120 121 /// <summary> Adds a new Asn1Object to the end of this Asn1Structured 122 /// object. 123 /// 124 /// </summary> 125 /// <param name="value">The Asn1Object to add to this Asn1Structured 126 /// object. 127 /// </param> add(Asn1Object value_Renamed)128 public void add(Asn1Object value_Renamed) 129 { 130 if (contentIndex == content.Length) 131 { 132 // Array too small, need to expand it, double length 133 int newSize = contentIndex + contentIndex; 134 Asn1Object[] newArray = new Asn1Object[newSize]; 135 Array.Copy((System.Array) content, 0, (System.Array) newArray, 0, contentIndex); 136 content = newArray; 137 } 138 content[contentIndex++] = value_Renamed; 139 return ; 140 } 141 142 /// <summary> Replaces the Asn1Object in the specified index position of 143 /// this Asn1Structured object. 144 /// 145 /// </summary> 146 /// <param name="index">The index into the Asn1Structured object where 147 /// this new ANS1Object will be placed. 148 /// 149 /// </param> 150 /// <param name="value">The Asn1Object to set in this Asn1Structured 151 /// object. 152 /// </param> set_Renamed(int index, Asn1Object value_Renamed)153 public void set_Renamed(int index, Asn1Object value_Renamed) 154 { 155 if ((index >= contentIndex) || (index < 0)) 156 { 157 throw new System.IndexOutOfRangeException("Asn1Structured: get: index " + index + ", size " + contentIndex); 158 } 159 content[index] = value_Renamed; 160 return ; 161 } 162 163 /// <summary> Gets a specific Asn1Object in this structred object. 164 /// 165 /// </summary> 166 /// <param name="index">The index of the Asn1Object to get from 167 /// this Asn1Structured object. 168 /// </param> get_Renamed(int index)169 public Asn1Object get_Renamed(int index) 170 { 171 if ((index >= contentIndex) || (index < 0)) 172 { 173 throw new System.IndexOutOfRangeException("Asn1Structured: set: index " + index + ", size " + contentIndex); 174 } 175 return content[index]; 176 } 177 178 /// <summary> Returns the number of Asn1Obejcts that have been encoded 179 /// into this Asn1Structured class. 180 /// </summary> size()181 public int size() 182 { 183 return contentIndex; 184 } 185 186 /// <summary> Creates a String representation of this Asn1Structured. 187 /// object. 188 /// 189 /// </summary> 190 /// <param name="type">the Type to put in the String representing this structured object 191 /// 192 /// </param> 193 /// <returns> the String representation of this object. 194 /// </returns> 195 [CLSCompliantAttribute(false)] toString(System.String type)196 public virtual System.String toString(System.String type) 197 { 198 System.Text.StringBuilder sb = new System.Text.StringBuilder(); 199 200 sb.Append(type); 201 202 for (int i = 0; i < contentIndex; i++) 203 { 204 sb.Append(content[i]); 205 if (i != contentIndex - 1) 206 sb.Append(", "); 207 } 208 sb.Append(" }"); 209 210 return base.ToString() + sb.ToString(); 211 } 212 } 213 } 214