ReadContentsAsBoolean() public method

Reads the contents of the current data value as a bool.
An error occurred while parsing the EmBER-encoded data, see /// for more information. /// /// The value of is not equal to , /// or /// The contents of the current data value has already been read. /// /// has been called.
public ReadContentsAsBoolean ( ) : bool
return bool
Ejemplo n.º 1
0
        public void SkipContentsTest()
        {
            using (var stream = new MemoryStream(new byte[] { 0x60, 0x03, 0x01, 0x01, 0xFF, 0x60, 0x03, 0x01, 0x01, 0x00 }))
                using (var reader = new EmberReader(stream, 1))
                {
                    Assert.IsTrue(reader.Read());
                    Assert.IsTrue(reader.Read());
                    Assert.IsFalse(reader.ReadContentsAsBoolean());
                }

            var original = new byte[64];

            this.Random.NextBytes(original);
            byte[] encoded;

            using (var stream = new MemoryStream())
            {
                using (var writer = new EmberWriter(stream))
                {
                    writer.WriteValue(EmberId.CreateApplication(0), original);
                    writer.WriteValue(EmberId.CreateApplication(1), true);
                }

                encoded = stream.ToArray();
            }

            using (var stream = new MemoryStream(encoded))
                using (var reader = new EmberReader(stream, 1))
                {
                    Assert.IsTrue(reader.Read());
                    Assert.AreEqual(InnerNumber.Octetstring, reader.InnerNumber);
                    Assert.IsTrue(reader.Read());
                    Assert.AreEqual(InnerNumber.Boolean, reader.InnerNumber);
                    Assert.AreEqual(true, reader.ReadContentsAsBoolean());
                    Assert.IsFalse(reader.Read());
                }
        }
Ejemplo n.º 2
0
        public void ExceptionTest()
        {
            TestStandardExceptionConstructors <EmberException>();

            AssertThrow <ArgumentNullException>(() => new EmberReader(null, 1).Dispose());
            AssertEmberException("Incorrect length at position 3.", 0x60, 0x03, 0x0D, 0x01, 0xFF, 0x00);

            using (var stream = new MemoryStream(new byte[] { 0x60, 0x03, 0x01, 0x01, 0xFF }))
                using (var reader = new EmberReader(stream, 1))
                {
                    reader.Read();
                    AssertThrow <InvalidOperationException>(() => reader.ReadContentsAsString());
                    reader.ReadContentsAsBoolean();
                }

            AssertEmberException(
                "Unexpected Universal class for outer identifier at position 0.", 0x01, 0x03, 0x01, 0x01, 0xFF);

            AssertEmberException("Unexpected end of stream.", 0x60);

            AssertEmberException("Unexpected end of stream.", 0x60, 0x05, 0x04, 0x03, 0xFF, 0xFE);

            AssertEmberException(
                "Unexpected end of stream at position 9.", 0x60, 0x08, 0x30, 0x06, 0x60, 0x03, 0x01, 0x01, 0xFF);

            AssertEmberException("Unexpected End-of-contents identifier at position 2.", 0x60, 0x02, 0x00, 0x00);

            AssertEmberException("Unexpected number in universal identifier at position 2.", 0x60, 0x03, 0x03, 0x01, 0xFF);

            AssertEmberException("Unexpected context-specific or private identifier at position 2.", 0x60, 0x03, 0x83, 0x01, 0xFF);
            AssertEmberException("Unexpected context-specific or private identifier at position 2.", 0x60, 0x03, 0xC3, 0x01, 0xFF);

            AssertEmberException("Unexpected length for End-of-contents identifier at position 0.", 0x00, 0x01);

            AssertEmberException("Unexpected excess End-of-contents identifier at position 0.", 0x00, 0x00);

            AssertEmberException(
                "Unexpected End-of-contents identifier at position 6 for definite length at position 1.",
                0x60, 0x06, 0x30, 0x80, 0x00, 0x00, 0x00, 0x00);

            AssertEmberException("Unexpected constructed encoding at position 2.", 0x60, 0x03, 0x21, 0x01, 0xFF);

            AssertEmberException(
                "Unexpected indefinite length for primitive data value at position 2.", 0x60, 0x03, 0x01, 0x80, 0xFF);

            AssertEmberException("Unexpected encoding for Real at position 4.", 0x60, 0x03, 0x09, 0x01, 0x00);

            AssertEmberException("Incorrect length for Real at position 4.", 0x60, 0x04, 0x09, 0x02, 0x80, 0x00);

            AssertEmberException(
                "The exponent of the Real at position 4 exceeds the expected range.",
                0x60, 0x06, 0x09, 0x04, 0x81, 0x04, 0x01, 0x01);

            AssertEmberException(
                "The exponent of the Real at position 4 exceeds the expected range.",
                0x60, 0x06, 0x09, 0x04, 0x81, 0xFC, 0x01, 0x01);

            AssertEmberException(
                "The mantissa of the Real at position 4 is zero.", 0x60, 0x05, 0x09, 0x03, 0x80, 0x00, 0x00);

            AssertEmberException(
                "The length at position 1 exceeds the expected range.", 0x60, 0x84, 0x80, 0x00, 0x00, 0x00);

            AssertEmberException("Unexpected zero length for integer.", 0x60, 0x02, 0x02, 0x00);

            ReadAll(0x60, 0x0A, 0x02, 0x08, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
            AssertEmberException(
                "The integer, length or exponent at position 4 exceeds the expected range.",
                0x60, 0x0B, 0x02, 0x09, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);

            ReadAll(0x60, 0x0A, 0x02, 0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
            AssertEmberException(
                "The integer, length or exponent at position 4 exceeds the expected range.",
                0x60, 0x0B, 0x02, 0x09, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);

            ReadAll(0x7F, 0x87, 0xFF, 0xFF, 0xFF, 0x7F, 0x03, 0x01, 0x01, 0xFF);
            AssertEmberException(
                "The identifier number or subidentifier at position 1 exceeds the expected range.",
                0x7F, 0x8F, 0xFF, 0xFF, 0xFF, 0x7F, 0x03, 0x01, 0x01, 0xFF);
        }
        private void ToXmlCore(
            EmberReader reader, XmlWriter writer, FieldPath <int, EmberId> previousPath, int currentType)
        {
            while (reader.Read())
            {
                var nextType = reader.InnerNumber;

                if (nextType == InnerNumber.EndContainer)
                {
                    writer.WriteEndElement();
                    return;
                }

                var currentPath = Combine(previousPath, new Field <int, EmberId>(currentType, reader.OuterId));
                var fieldName   = this.GetFieldName(currentPath);

                switch (nextType)
                {
                case BerBoolean.InnerNumber:
                    var boolean = reader.ReadContentsAsBoolean();
                    WriteValue(writer, fieldName, BerBoolean.Name, boolean, (w, o) => w.WriteValue(o));
                    break;

                case BerInteger.InnerNumber:
                    var integer = reader.ReadContentsAsInt64();
                    WriteValue(writer, fieldName, BerInteger.Name, integer, (w, o) => w.WriteValue(o));
                    break;

                case BerReal.InnerNumber:
                    WriteValue(
                        writer, fieldName, BerReal.Name, reader.ReadContentsAsDouble(), (w, o) => w.WriteValue(o));
                    break;

                case BerUtf8String.InnerNumber:
                    string str;

                    try
                    {
                        str = XmlConvert.VerifyXmlChars(reader.ReadContentsAsString());
                    }
                    catch (XmlException)
                    {
                        str = "*** CONTAINS INVALID XML CHARACTERS ***";
                    }

                    switch (currentPath.ToString())
                    {
                    case "1073741825.C-1.C-6":
                    case "1073741825.C-1.C-7":
                    case "1073741825.C-1.C-17":
                    case "1073741833.C-1.C-6":
                    case "1073741833.C-1.C-7":
                    case "1073741833.C-1.C-17":
                    case "1073741827.C-1.C-4":
                    case "1073741834.C-1.C-4":
                    case "1073741837.C-1.C-11":
                    case "1073741841.C-1.C-11":
                        str = str.Replace("\n", Environment.NewLine);
                        break;

                    default:
                        // Intentionally do nothing
                        break;
                    }

                    WriteValue(writer, fieldName, BerUtf8String.Name, str, (w, o) => w.WriteValue(o));
                    break;

                case BerOctetstring.InnerNumber:
                    var bytes = reader.ReadContentsAsByteArray();
                    WriteValue(
                        writer, fieldName, BerOctetstring.Name, bytes, (w, o) => w.WriteBinHex(o, 0, o.Length));
                    break;

                case BerRelativeObjectIdentifier.InnerNumber:
                    var intOid = reader.ReadContentsAsInt32Array();
                    var oid    = string.Join(".", intOid.Select(e => e.ToString(InvariantCulture)));
                    WriteValue(writer, fieldName, BerRelativeObjectIdentifier.Name, oid, (w, o) => w.WriteValue(o));
                    break;

                case BerSequence.InnerNumber:
                    WriteStartContainer(writer, fieldName, BerSequence.Name);
                    this.ToXmlCore(reader, writer, currentPath, nextType);
                    break;

                case BerSet.InnerNumber:
                    WriteStartContainer(writer, fieldName, BerSet.Name);
                    this.ToXmlCore(reader, writer, currentPath, nextType);
                    break;

                default:
                    WriteStartContainer(writer, fieldName, this.GetTypeName(nextType));
                    this.ToXmlCore(reader, writer, currentPath, nextType);
                    break;
                }
            }
        }