public static string DecodeAsciiString(IBerInput input, int length) { var intLength = (int)length; var bytes = ReadString(input, ref intLength); return(AsciiEncoding.GetString(bytes, 0, intLength)); }
public static DateTime DecodeGeneralizedTime(IBerInput input, int length) { var str = DecodeAsciiString(input, length); int year, month, day, hour, minute, second, millisecond; var isUtc = false; try { year = int.Parse(str.Substring(0, 4)); month = int.Parse(str.Substring(4, 2)); day = int.Parse(str.Substring(6, 2)); hour = int.Parse(str.Substring(8, 2)); minute = int.Parse(str.Substring(10, 2)); second = int.Parse(str.Substring(12, 2)); var suffix = str.Substring(15); if(suffix.EndsWith("Z")) // UTC { suffix = suffix.Substring(0, suffix.Length - 1); isUtc = true; } millisecond = int.Parse(suffix); return new DateTime(year, month, day, hour, minute, second, millisecond, isUtc ? DateTimeKind.Utc : DateTimeKind.Local); } catch(Exception ex) { Debug.WriteLine(ex); return DateTime.MinValue; } }
public static string DecodeAsciiString(IBerInput input, int length) { var intLength = (int)length; var bytes = ReadString(input, ref intLength); return AsciiEncoding.GetString(bytes, 0, intLength); }
public static int DecodeLength(IBerInput input) { int value; int byteCount; value = input.ReadByte(); if ((value & 0x80) != 0) { byteCount = value & 0x7F; if (byteCount == 0) { value = BerDefinitions.IndefiniteLength; // indefinite length form } else { value = 0; for ( ; byteCount > 0; byteCount--) { value = (value << 8) | input.ReadByte(); } } } return(value); }
public static double DecodeReal(IBerInput input, int length) { double value = 0; if (length != 0) { byte preamble = input.ReadByte(); if (length == 1 && preamble == 0x40) // positive infinity { value = double.PositiveInfinity; } else if (length == 1 && preamble == 0x41) // negative infinity { value = double.NegativeInfinity; } else if (length == 1 && preamble == 0x42) // not a number { value = double.NaN; } else { long longValue; int exponentLength = 1 + (preamble & 3); int sign = preamble & 0x40; int ff = (preamble >> 2) & 3; // Unpack mantissa & decrement exponent for base 2 long exponent = DecodeLong(input, exponentLength); long mantissa = DecodeLong(input, length - exponentLength - 1, false) << ff; // de-normalize mantissa (required by CER and DER) while ((mantissa & 0x7FFFF00000000000L) == 0x0) { mantissa <<= 8; } while ((mantissa & 0x7FF0000000000000L) == 0x0) { mantissa <<= 1; } mantissa &= 0x0FFFFFFFFFFFFFL; longValue = (exponent + 1023) << 52; longValue |= mantissa; if (sign != 0) { ulong qword = (ulong)longValue | 0x8000000000000000UL; longValue = (long)qword; } value = Int64BitsToDouble((long)longValue); } } return(value); }
public static int[] DecodeObjectIdentifier(IBerInput input, int length) { var values = new List <int>(); int subidentifier = 0; uint bytesRead = 0; while (bytesRead++ < length) { byte uByte = input.ReadByte(); subidentifier = ((subidentifier << 7) | (uByte & 0x7F)); if ((uByte & 0x80) == 0) { if (values.Count > 0) { values.Add(subidentifier); } else { values.Add(subidentifier / 40); values.Add(subidentifier % 40); } subidentifier = 0; } } return(values.ToArray()); }
public static DateTime DecodeGeneralizedTime(IBerInput input, int length) { var str = DecodeAsciiString(input, length); int year, month, day, hour, minute, second, millisecond; var isUtc = false; try { year = int.Parse(str.Substring(0, 4)); month = int.Parse(str.Substring(4, 2)); day = int.Parse(str.Substring(6, 2)); hour = int.Parse(str.Substring(8, 2)); minute = int.Parse(str.Substring(10, 2)); second = int.Parse(str.Substring(12, 2)); var suffix = str.Substring(15); if (suffix.EndsWith("Z")) // UTC { suffix = suffix.Substring(0, suffix.Length - 1); isUtc = true; } millisecond = int.Parse(suffix); return(new DateTime(year, month, day, hour, minute, second, millisecond, isUtc ? DateTimeKind.Utc : DateTimeKind.Local)); } catch (Exception ex) { Debug.WriteLine($"Exception: BerLib/BerEncoding/DecodeGeneralizedTime: {ex.Message}"); return(DateTime.MinValue); } }
public static byte[] DecodeByteArray(IBerInput input, int length) { var bytes = new byte[length]; for(int index = 0; index < length; index++) bytes[index] = input.ReadByte(); return bytes; }
public static byte[] DecodeByteArray(IBerInput input, int length) { var bytes = new byte[length]; for (int index = 0; index < length; index++) { bytes[index] = input.ReadByte(); } return(bytes); }
static int DecodeHeader(IBerInput input, BerTag expectedTag) { var tag = BerEncoding.DecodeTag(input); if (tag != expectedTag) { throw new BerException(4001, String.Format("Expected tag {0}, found tag {1}", tag, expectedTag)); } return(BerEncoding.DecodeLength(input)); }
// ==================================================================== // // Decode functions // // ==================================================================== #region DecodeFunctions public static BerTag DecodeTag(IBerInput input) { var tagByte = input.ReadByte(); var tag = new BerTag((byte)(tagByte & 0xE0), (uint)(tagByte & 0x1F)); if (tag.Number == 0x1F) { tag.Number = DecodeMultiByteInteger(input); } return(tag); }
/// <summary> /// Creates a nested reader from another EmberReader instance. /// The passed <paramref name="parentReader"/> must be positioned /// on a container. /// The newly created nested reader will signal Eof when all bytes /// of this container have been read. /// </summary> /// <param name="parentReader">The base reader.</param> public EmberReader(EmberReader parentReader) { if(parentReader == null) throw new ArgumentNullException("parentReader"); if(parentReader.IsContainer == false) throw new ArgumentException("parentReader is not positioned on a container"); _input = parentReader._input; _parentReader = parentReader; _bytesAvailable = parentReader.Length; }
public static ulong DecodeMultiByteLong(IBerInput input) { byte currentByte; ulong value = 0; do { currentByte = input.ReadByte(); value = (value << 7) | (byte)(currentByte & ~0x80); } while((currentByte & 0x80) != 0); return(value); }
public static int[] DecodeRelativeOid(IBerInput input, int length) { var subidentifiers = new List <int>(); while (length > 0) { int count; subidentifiers.Add((int)DecodeMultiByteInteger(input, out count)); length -= count; } return(subidentifiers.ToArray()); }
public static uint DecodeMultiByteInteger(IBerInput input, out int consumedByteCount) { byte currentByte; uint value = 0; var count = 0; do { currentByte = input.ReadByte(); value = (value << 7) | (byte)(currentByte & ~0x80); count++; } while((currentByte & 0x80) != 0); consumedByteCount = count; return(value); }
public static int DecodeInteger(IBerInput input, int length) { int value = 0; int readByte; for (uint byteCount = 0; byteCount < length; byteCount++) { readByte = input.ReadByte(); if (byteCount == 0 && (readByte & 0x80) != 0) { readByte -= 0x100; } value = (value << 8) | readByte; } return(value); }
static byte[] ReadString(IBerInput input, ref int length) { var bytes = new byte[length]; if (length > 0) { for (int nIndex = 0; nIndex < length; nIndex++) { bytes[nIndex] = input.ReadByte(); } // strip trailing zeroes while (length > 0 && bytes[length - 1] == 0) { length--; } } return(bytes); }
static long DecodeLong(IBerInput input, int length, bool isSigned) { long value = 0; long readByte; for (uint byteCount = 0; byteCount < length; byteCount++) { readByte = input.ReadByte(); if (byteCount == 0 && (readByte & 0x80) != 0 && isSigned) { readByte -= 0x100; } value = (value << 8) | readByte; } return(value); }
public static long DecodeLong(IBerInput input, int length) { return DecodeLong(input, length, true); }
public static int DecodeLength(IBerInput input) { int value; int byteCount; value = input.ReadByte(); if((value & 0x80) != 0) { byteCount = value & 0x7F; if(byteCount == 0) { value = BerDefinitions.IndefiniteLength; // indefinite length form } else { value = 0; for( ; byteCount > 0; byteCount--) value = (value << 8) | input.ReadByte(); } } return value; }
public static uint DecodeMultiByteInteger(IBerInput input) { int count; return DecodeMultiByteInteger(input, out count); }
public static uint DecodeMultiByteInteger(IBerInput input, out int consumedByteCount) { byte currentByte; uint value = 0; var count = 0; do { currentByte = input.ReadByte(); value = (value << 7) | (byte)(currentByte & ~0x80); count++; } while((currentByte & 0x80) != 0); consumedByteCount = count; return value; }
static long DecodeLong(IBerInput input, int length, bool isSigned) { long value = 0; long readByte; for(uint byteCount = 0; byteCount < length; byteCount++) { readByte = input.ReadByte(); if(byteCount == 0 && (readByte & 0x80) != 0 && isSigned) readByte -= 0x100; value = (value << 8) | readByte; } return value; }
public static long DecodeLong(IBerInput input, int length) { return(DecodeLong(input, length, true)); }
public static int[] DecodeObjectIdentifier(IBerInput input, int length) { var values = new List<int>(); int subidentifier = 0; uint bytesRead = 0; while(bytesRead++ < length) { byte uByte = input.ReadByte(); subidentifier = ((subidentifier << 7) | (uByte & 0x7F)); if((uByte & 0x80) == 0) { if(values.Count > 0) { values.Add(subidentifier); } else { values.Add(subidentifier / 40); values.Add(subidentifier % 40); } subidentifier = 0; } } return values.ToArray(); }
static int DecodeHeader(IBerInput input, BerTag expectedTag) { var tag = BerEncoding.DecodeTag(input); if(tag != expectedTag) throw new BerException(4001, String.Format("Expected tag {0}, found tag {1}", tag, expectedTag)); return BerEncoding.DecodeLength(input); }
public static double DecodeReal(IBerInput input, int length) { double value = 0; if(length != 0) { byte preamble = input.ReadByte(); if(length == 1 && preamble == 0x40) // positive infinity { value = double.PositiveInfinity; } else if(length == 1 && preamble == 0x41) // negative infinity { value = double.NegativeInfinity; } else { long longValue; int exponentLength = 1 + (preamble & 3); int sign = preamble & 0x40; int ff = (preamble >> 2) & 3; // Unpack mantissa & decrement exponent for base 2 long exponent = DecodeLong(input, exponentLength); long mantissa = DecodeLong(input, length - exponentLength - 1, false) << ff; // de-normalize mantissa (required by CER and DER) while((mantissa & 0x7FFFF00000000000L) == 0x0) mantissa <<= 8; while((mantissa & 0x7FF0000000000000L) == 0x0) mantissa <<= 1; mantissa &= 0x0FFFFFFFFFFFFFL; longValue = (exponent + 1023) << 52; longValue |= mantissa; if(sign != 0) { ulong qword = (ulong)longValue | 0x8000000000000000UL; longValue = (long)qword; } value = Int64BitsToDouble((long)longValue); } } return value; }
public static uint DecodeMultiByteInteger(IBerInput input) { int count; return(DecodeMultiByteInteger(input, out count)); }
public static int[] DecodeRelativeOid(IBerInput input, int length) { var subidentifiers = new List<int>(); while(length > 0) { int count; subidentifiers.Add((int)DecodeMultiByteInteger(input, out count)); length -= count; } return subidentifiers.ToArray(); }
// ==================================================================== // // Decode functions // // ==================================================================== public static BerTag DecodeTag(IBerInput input) { var tagByte = input.ReadByte(); var tag = new BerTag((byte)(tagByte & 0xE0), (uint)(tagByte & 0x1F)); if(tag.Number == 0x1F) tag.Number = DecodeMultiByteInteger(input); return tag; }
static byte[] ReadString(IBerInput input, ref int length) { var bytes = new byte[length]; if(length > 0) { for(int nIndex = 0; nIndex < length; nIndex++) bytes[nIndex] = input.ReadByte(); // strip trailing zeroes while(length > 0 && bytes[length - 1] == 0) length--; } return bytes; }
public static int DecodeInteger(IBerInput input, int length) { int value = 0; int readByte; for(uint byteCount = 0; byteCount < length; byteCount++) { readByte = input.ReadByte(); if(byteCount == 0 && (readByte & 0x80) != 0) readByte -= 0x100; value = (value << 8) | readByte; } return value; }
public static bool DecodeBoolean(IBerInput input) { return(input.ReadByte() != 0); }
/// <summary> /// Creates a new EmberReader instance configured to read BER encoded data /// from <paramref name="input"/>. /// </summary> /// <param name="input">The input to read BER encoded data from.</param> public EmberReader(IBerInput input) { Debug.Assert(input != null); _input = input; }
public static ulong DecodeMultiByteLong(IBerInput input) { byte currentByte; ulong value = 0; do { currentByte = input.ReadByte(); value = (value << 7) | (byte)(currentByte & ~0x80); } while((currentByte & 0x80) != 0); return value; }
public static bool DecodeBoolean(IBerInput input) { return input.ReadByte() != 0; }