2using System.Collections.Generic;
13 public static class BER
24 int i = Input.ReadByte();
26 throw new EndOfStreamException();
29 Constructed = (i & 32) != 0;
35 return DecodeVarLenInt(Input);
38 private static long DecodeVarLenInt(Stream Input)
40 int i = Input.ReadByte();
42 throw new EndOfStreamException();
44 long Result = (uint)(i & 127);
49 throw new EndOfStreamException();
52 Result |= (uint)(i & 127);
65 int i = Input.ReadByte();
67 throw new EndOfStreamException();
78 throw new NotSupportedException(
"Too long.");
80 return ReadInteger(Input, i,
false);
83 private static long ReadInteger(Stream Input,
int NrBytes,
bool Signed)
93 throw new EndOfStreamException();
98 if (Signed && (i & 128) != 0)
117 throw new InvalidOperationException(
"Invalid BOOLEAN value.");
119 int i = Input.ReadByte();
121 throw new EndOfStreamException();
135 throw new NotSupportedException(
"Indefinite length not supported.");
138 throw new NotSupportedException(
"INTEGER too large.");
143 return ReadInteger(Input, (
int)Len,
true);
156 return (T)((object)i);
171 throw new NotSupportedException(
"Indefinite length not supported.");
174 throw new NotSupportedException(
"REAL size not supported.");
176 int i = Input.ReadByte();
178 throw new EndOfStreamException();
180 int c = (int)(Len - 1);
184 bool Negative = (i & 64) != 0;
185 byte F = (byte)((i >> 2) & 3);
192 Exponent = ReadInteger(Input, 1,
true);
197 Exponent = ReadInteger(Input, 2,
true);
202 Exponent = ReadInteger(Input, 3,
true);
207 int j = (int)ReadInteger(Input, 1,
false);
209 Exponent = ReadInteger(Input, j,
true);
214 throw new InvalidOperationException(
"Invalid REAL.");
216 double Mantissa = ReadInteger(Input, c,
false);
218 Mantissa *= Math.Pow(2.0, F);
220 Mantissa = -Mantissa;
225 switch ((i >> 4) & 3)
228 return Mantissa * Math.Pow(2, Exponent);
231 return Mantissa * Math.Pow(8, Exponent);
234 return Mantissa * Math.Pow(16, Exponent);
238 throw new NotSupportedException(
"Reserved base.");
241 else if ((i & 64) == 0)
243 byte[] Bin =
new byte[c];
244 Input.ReadAll(Bin, 0, c);
249 throw new NotSupportedException(
"Unsupported REAL value.");
256 throw new NotSupportedException(
"Invalid special value");
258 i = Input.ReadByte();
260 throw new EndOfStreamException();
264 case 64:
return double.PositiveInfinity;
265 case 65:
return double.NegativeInfinity;
266 case 66:
return double.NaN;
268 default:
throw new NotSupportedException(
"Invalid special value");
283 throw new InvalidOperationException(
"Invalid BIT STRING.");
287 using (MemoryStream ms =
new MemoryStream())
293 throw new NotSupportedException(
"Expected BIT STRING.");
296 ms.Write(Bin, 0, Bin.Length);
300 throw new InvalidOperationException(
"Expected zero length.");
307 NrUnusedBits = Input.ReadByte();
308 if (NrUnusedBits < 0)
309 throw new EndOfStreamException();
311 if (--Len >
int.MaxValue)
312 throw new NotSupportedException(
"BIT STRING too large.");
315 byte[] Bin =
new byte[c];
316 Input.ReadAll(Bin, 0, c);
335 using (MemoryStream ms =
new MemoryStream())
340 throw new NotSupportedException(
"Expected OCTET STRING.");
343 ms.Write(Bin, 0, Bin.Length);
347 throw new InvalidOperationException(
"Expected zero length.");
354 if (Len >
int.MaxValue)
355 throw new NotSupportedException(
"OCTET STRING too large.");
358 byte[] Bin =
new byte[c];
359 Input.ReadAll(Bin, 0, c);
373 throw new InvalidOperationException(
"Expected zero length.");
388 throw new NotSupportedException(
"Indefinite length not supported.");
390 List<int> Result =
new List<int>();
391 long EndPos = Input.Position + Len;
393 Len = DecodeVarLenInt(Input);
394 Result.Add((
int)(Len % 40));
397 if (Len >
int.MaxValue)
398 throw new NotSupportedException(
"Invalid OBJECT IDENTIFIER");
400 Result.Add((
int)Len);
402 while (Input.Position < EndPos)
404 Len = DecodeVarLenInt(Input);
405 if (Len >
int.MaxValue)
406 throw new NotSupportedException(
"Invalid OBJECT IDENTIFIER");
408 Result.Add((
int)Len);
411 return Result.ToArray();
512 throw new NotSupportedException(
"Unsupported DATE format.");
535 throw new NotSupportedException(
"Unsupported DATE-TIME format.");
548 throw new NotSupportedException(
"Unsupported DURATION format.");
Implements static methods for Basic Encoding Rules (BER), as defined in X.690
static long DecodeIdentifier(Stream Input, out bool Constructed, out TagClass Class)
Decodes an identifier from the stream.
static TimeSpan DecodeTime(Stream Input)
Decodes a TIME value.
static TimeSpan DecodeTimeOfDay(Stream Input)
Decodes a TIME-OF-DAY value.
static void DecodeNull(Stream Input)
Decodes a NULL value.
static string DecodeIa5String(Stream Input)
Decodes a IA5String value.
static string DecodeVisibleString(Stream Input)
Decodes a VisibleString value.
static DateTime DecodeDateTime(Stream Input)
Decodes a DATE-TIME value.
static long DecodeINTEGER(Stream Input)
Decodes an INTEGER value.
static string DecodePrintableString(Stream Input)
Decodes a PrintableString value.
static long DecodeLength(Stream Input)
Decodes the length of a contents section.
static DateTime DecodeDate(Stream Input)
Decodes a DATE value.
static string DecodeBmpString(Stream Input)
Decodes a BmpString value.
static Duration DecodeDuration(Stream Input)
Decodes a DURATION value.
static byte[] DecodeBitString(Stream Input, out int NrUnusedBits)
Decodes a BIT STRING value.
static Enum DecodeEnum< T >(Stream Input)
Decodes an enumerated value.
static int[] DecodeRelativeObjectId(Stream Input)
Decodes a RELATIVE-OID value.
static double DecodeREAL(Stream Input)
Decodes a REAL value.
static string DecodeNumericString(Stream Input)
Decodes a NumericString value.
static bool DecodeBOOLEAN(Stream Input)
Decodes a BOOLEAN value.
static string DecodeUniversalString(Stream Input)
Decodes a UniversalString value.
static byte[] DecodeOctetString(Stream Input)
Decodes a OCTET STRING value.
static string DecodeUtf8String(Stream Input)
Decodes a Utf8String value.
static int[] DecodeObjectId(Stream Input)
Decodes an OBJECT IDENTIFIER value.
Helps with parsing of commong data types.
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Helps with common XML-related tasks.
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Represents a duration value, as defined by the xsd:duration data type: http://www....
static bool TryParse(string s, out Duration Result)
Tries to parse a duration value.