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 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); }
// ==================================================================== // // 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); }
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 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); }
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 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 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 bool DecodeBoolean(IBerInput input) { return(input.ReadByte() != 0); }
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[] 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 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; }
// ==================================================================== // // 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; }
public static bool DecodeBoolean(IBerInput input) { return input.ReadByte() != 0; }
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; }
byte ReadByte() { _bytesRead++; return(_input.ReadByte()); }