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.Asn1OctetString.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 encapsulates the OCTET STRING type.</summary>
38 [CLSCompliantAttribute(true)]
39 public class Asn1OctetString
:Asn1Object
42 private sbyte[] content
;
44 /// <summary> ASN.1 OCTET STRING tag definition.</summary>
45 public const int TAG
= 0x04;
47 /// <summary> ID is added for Optimization.
48 /// Id needs only be one Value for every instance,
49 /// thus we create it only once.
51 protected internal static readonly Asn1Identifier ID
= new Asn1Identifier(Asn1Identifier
.UNIVERSAL
, false, TAG
);
52 /* Constructors for Asn1OctetString
55 /// <summary> Call this constructor to construct an Asn1OctetString
56 /// object from a byte array.
59 /// <param name="content">A byte array representing the string that
60 /// will be contained in the this Asn1OctetString object
62 [CLSCompliantAttribute(false)]
63 public Asn1OctetString(sbyte[] content
):base(ID
)
65 this.content
= content
;
70 /// <summary> Call this constructor to construct an Asn1OctetString
71 /// object from a String object.
74 /// <param name="content">A string value that will be contained
75 /// in the this Asn1OctetString object
77 public Asn1OctetString(System
.String content
):base(ID
)
81 /* System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
82 byte[] bytes = utf8.GetBytes (content);
83 sbyte[] sbytes = new sbyte[bytes.Length+1]; //signed bytes
84 sbytes[0] = 0; //set sign byte to zero.
85 for(int i=1; i<sbytes.Length; i++)
86 sbytes[i] = (sbyte) bytes[i-1]; //cast byte-->sbyte
88 System
.Text
.Encoding encoder
= System
.Text
.Encoding
.GetEncoding("utf-8");
89 byte[] ibytes
= encoder
.GetBytes(content
);
90 sbyte[] sbytes
=SupportClass
.ToSByteArray(ibytes
);
92 this.content
= sbytes
;
93 // this.content = content.getBytes("UTF8");
95 catch (System
.IO
.IOException uee
)
97 throw new System
.SystemException(uee
.ToString());
103 /// <summary> Constructs an Asn1OctetString object by decoding data from an
107 /// <param name="dec">The decoder object to use when decoding the
108 /// input stream. Sometimes a developer might want to pass
109 /// in his/her own decoder object
112 /// <param name="in">A byte stream that contains the encoded ASN.1
115 [CLSCompliantAttribute(false)]
116 public Asn1OctetString(Asn1Decoder dec
, System
.IO
.Stream in_Renamed
, int len
):base(ID
)
118 content
= (len
> 0)?(sbyte[]) dec
.decodeOctetString(in_Renamed
, len
):new sbyte[0];
123 /* Asn1Object implementation
126 /// <summary> Call this method to encode the current instance into the
127 /// specified output stream using the specified encoder object.
130 /// <param name="enc">Encoder object to use when encoding self.
133 /// <param name="out">The output stream onto which the encoded byte
134 /// stream is written.
136 public override void encode(Asn1Encoder enc
, System
.IO
.Stream out_Renamed
)
138 enc
.encode(this, out_Renamed
);
143 /*Asn1OctetString specific methods
146 /// <summary> Returns the content of this Asn1OctetString as a byte array.</summary>
147 [CLSCompliantAttribute(false)]
148 public sbyte[] byteValue()
154 /// <summary> Returns the content of this Asn1OctetString as a String.</summary>
155 public System
.String
stringValue()
157 System
.String s
= null;
160 System
.Text
.Encoding encoder
= System
.Text
.Encoding
.GetEncoding("utf-8");
161 char[] dchar
= encoder
.GetChars(SupportClass
.ToByteArray(content
));
162 s
= new String(dchar
);
163 // sbyte *sb=content;
164 // s = new String(sb,0,content.Length, new System.Text.UTF8Encoding());
166 catch (System
.IO
.IOException uee
)
168 throw new System
.SystemException(uee
.ToString());
175 /// <summary> Return a String representation of this Asn1Object.</summary>
176 public override System
.String
ToString()
178 return base.ToString() + "OCTET STRING: " + stringValue();