| 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.Asn1OctetString.cs |
| 25 |
// |
| 26 |
// Author: |
| 27 |
// Sunil Kumar ([email protected]) |
| 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 encapsulates the OCTET STRING type.</summary> |
| 38 |
public class Asn1OctetString:Asn1Object |
| 39 |
{ |
| 40 |
|
| 41 |
private sbyte[] content; |
| 42 |
|
| 43 |
/// <summary> ASN.1 OCTET STRING tag definition.</summary> |
| 44 |
public const int TAG = 0x04; |
| 45 |
|
| 46 |
/// <summary> ID is added for Optimization. |
| 47 |
/// Id needs only be one Value for every instance, |
| 48 |
/// thus we create it only once. |
| 49 |
/// </summary> |
| 50 |
protected internal static readonly Asn1Identifier ID = new Asn1Identifier(Asn1Identifier.UNIVERSAL, false, TAG); |
| 51 |
/* Constructors for Asn1OctetString |
| 52 |
*/ |
| 53 |
|
| 54 |
/// <summary> Call this constructor to construct an Asn1OctetString |
| 55 |
/// object from a byte array. |
| 56 |
/// |
| 57 |
/// </summary> |
| 58 |
/// <param name="content">A byte array representing the string that |
| 59 |
/// will be contained in the this Asn1OctetString object |
| 60 |
/// </param> |
| 61 |
[CLSCompliantAttribute(false)] |
| 62 |
public Asn1OctetString(sbyte[] content):base(ID) |
| 63 |
{ |
| 64 |
this.content = content; |
| 65 |
return ; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
/// <summary> Call this constructor to construct an Asn1OctetString |
| 70 |
/// object from a String object. |
| 71 |
/// |
| 72 |
/// </summary> |
| 73 |
/// <param name="content">A string value that will be contained |
| 74 |
/// in the this Asn1OctetString object |
| 75 |
/// </param> |
| 76 |
public Asn1OctetString(System.String content):base(ID) |
| 77 |
{ |
| 78 |
try |
| 79 |
{ |
| 80 |
/* System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding(); |
| 81 |
byte[] bytes = utf8.GetBytes (content); |
| 82 |
sbyte[] sbytes = new sbyte[bytes.Length+1]; //signed bytes |
| 83 |
sbytes[0] = 0; //set sign byte to zero. |
| 84 |
for(int i=1; i<sbytes.Length; i++) |
| 85 |
sbytes[i] = (sbyte) bytes[i-1]; //cast byte-->sbyte |
| 86 |
*/ |
| 87 |
System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("utf-8"); |
| 88 |
byte[] ibytes = encoder.GetBytes(content); |
| 89 |
sbyte[] sbytes=SupportClass.ToSByteArray(ibytes); |
| 90 |
|
| 91 |
this.content = sbytes; |
| 92 |
// this.content = content.getBytes("UTF8"); |
| 93 |
} |
| 94 |
catch (System.IO.IOException uee) |
| 95 |
{ |
| 96 |
throw new System.SystemException(uee.ToString()); |
| 97 |
} |
| 98 |
return ; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/// <summary> Constructs an Asn1OctetString object by decoding data from an |
| 103 |
/// input stream. |
| 104 |
/// |
| 105 |
/// </summary> |
| 106 |
/// <param name="dec">The decoder object to use when decoding the |
| 107 |
/// input stream. Sometimes a developer might want to pass |
| 108 |
/// in his/her own decoder object |
| 109 |
/// |
| 110 |
/// </param> |
| 111 |
/// <param name="in">A byte stream that contains the encoded ASN.1 |
| 112 |
/// |
| 113 |
/// </param> |
| 114 |
[CLSCompliantAttribute(false)] |
| 115 |
public Asn1OctetString(Asn1Decoder dec, System.IO.Stream in_Renamed, int len):base(ID) |
| 116 |
{ |
| 117 |
content = (len > 0)?(sbyte[]) dec.decodeOctetString(in_Renamed, len):new sbyte[0]; |
| 118 |
return ; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/* Asn1Object implementation |
| 123 |
*/ |
| 124 |
|
| 125 |
/// <summary> Call this method to encode the current instance into the |
| 126 |
/// specified output stream using the specified encoder object. |
| 127 |
/// |
| 128 |
/// </summary> |
| 129 |
/// <param name="enc">Encoder object to use when encoding self. |
| 130 |
/// |
| 131 |
/// </param> |
| 132 |
/// <param name="out">The output stream onto which the encoded byte |
| 133 |
/// stream is written. |
| 134 |
/// </param> |
| 135 |
public override void encode(Asn1Encoder enc, System.IO.Stream out_Renamed) |
| 136 |
{ |
| 137 |
enc.encode(this, out_Renamed); |
| 138 |
return ; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/*Asn1OctetString specific methods |
| 143 |
*/ |
| 144 |
|
| 145 |
/// <summary> Returns the content of this Asn1OctetString as a byte array.</summary> |
| 146 |
[CLSCompliantAttribute(false)] |
| 147 |
public sbyte[] byteValue() |
| 148 |
{ |
| 149 |
return content; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/// <summary> Returns the content of this Asn1OctetString as a String.</summary> |
| 154 |
public System.String stringValue() |
| 155 |
{ |
| 156 |
System.String s = null; |
| 157 |
try |
| 158 |
{ |
| 159 |
System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("utf-8"); |
| 160 |
char[] dchar = encoder.GetChars(SupportClass.ToByteArray(content)); |
| 161 |
s = new String(dchar); |
| 162 |
// sbyte *sb=content; |
| 163 |
// s = new String(sb,0,content.Length, new System.Text.UTF8Encoding()); |
| 164 |
} |
| 165 |
catch (System.IO.IOException uee) |
| 166 |
{ |
| 167 |
throw new System.SystemException(uee.ToString()); |
| 168 |
} |
| 169 |
|
| 170 |
return s; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
/// <summary> Return a String representation of this Asn1Object.</summary> |
| 175 |
public override System.String ToString() |
| 176 |
{ |
| 177 |
return base.ToString() + "OCTET STRING: " + stringValue(); |
| 178 |
} |
| 179 |
} |
| 180 |
} |