void Test_Real2() { var encoded = new byte[] { 0xC0, 0x04, 0xDF }; var input = new BerMemoryInput(encoded); var decoded = BerEncoding.DecodeReal(input, encoded.Length); Console.WriteLine("decoded={0}", decoded); var output = new BerMemoryOutput(); var reencoded = BerEncoding.EncodeReal(output, decoded); var bytes = output.ToArray(); Console.WriteLine("reencoded={0}", BytesToString(bytes)); }
void Test_Real() { var values = new[] { 32.1, 32.125, 32.123, 100, 200, 300, -1000, 5.5005005, 777777777.123456789 }; foreach (var value in values) { var output = new BerMemoryOutput(); BerEncoding.EncodeReal(output, value); var input = new BerMemoryInput(output.Memory); var decodedValue = BerEncoding.DecodeReal(input, output.Length); Console.WriteLine("value={0} decoded={1}", value, decodedValue); } }
void AssertCodecSanity(EmberNode ember) { var originalXml = GetXml(ember); var output = new BerMemoryOutput(); ember.Encode(output); var input = new BerMemoryInput(output.Memory); var reader = new EmberReader(input); var decoded = EmberNode.Decode(reader, new GlowApplicationInterface()); var decodedXml = GetXml(decoded); if (originalXml != decodedXml) { throw new Exception("Codec error!"); } }
private void AssertEqual <T>( Action <EmberWriter, EmberId, T> write, Func <EmberLib.EmberReader, T> read, T value, Action <T, T> assertEqual) { var number = this.Random.Next(); var outer = EmberId.CreateApplication(number); var tag = new BerTag(BerClass.Application, (uint)number); MemoryStream output; using (output = new MemoryStream()) using (var writer = new EmberWriter(output, 1)) { write(writer, outer, value); } var input = new BerMemoryInput(output.ToArray()); var reader = new EmberLib.EmberReader(input); Assert.IsTrue(reader.Read()); Assert.AreEqual(tag, reader.Tag); assertEqual(value, read(reader)); }
bool ReadByte_Length(byte b) { if (_bytesExpected == 0) { if ((b & 0x80) != 0) { _bytesExpected = (b & 0x7F) + 1; } else { _bytesExpected = 1; } if (_bytesExpected > 5) { ThrowError(104, "Number of length octets out of bounds"); } } _bytesRead++; if (_bytesRead == _bytesExpected) { var input = new BerMemoryInput(_buffer); if (Type == 0) { OuterLength = BerEncoding.DecodeLength(input); if (OuterLength == 0) { ThrowError(102, "Zero outer length encountered"); } ResetState(DecodeState.Tag); } else { Length = BerEncoding.DecodeLength(input); var isEofOK = Length == 0; if (IsContainer) { ResetState(DecodeState.Tag); OnNewContainer(); PushContainer(); DisposeCurrentTlv(); return(isEofOK); } if (Length == 0) { OnValueReady(); } else { ResetState(DecodeState.Value); _buffer.Capacity = (int)Length; } return(isEofOK); } } return(false); }
bool ReadByte_Tag(byte b) { if (b == 0 && _bytesRead == 0) { ResetState(DecodeState.Terminator); return(false); } if (_bytesRead > 12) { ThrowError(103, "Number of tag octets out of bounds"); } if (_bytesRead == 0 && (b & 0x1F) != 0x1F || _bytesRead > 0 && (b & 0x80) == 0) { var input = new BerMemoryInput(_buffer); if (Tag.IsZero) { // no outer tag read yet -> read outer tag var tag = BerEncoding.DecodeTag(input); if (tag.Class == BerClass.Universal) { ThrowError(107, "Universal outer tag encountered"); } if (tag.IsContainer == false) { ThrowError(108, "Primitive outer tag encountered"); } Tag = tag.ToPrimitive(); // clear container flag } else { // outer tag already read -> read inner tag (type tag) var typeTag = BerEncoding.DecodeTag(input); var type = typeTag.NumberAsType; if (BerType.IsApplicationDefined(type) == false) { if (typeTag.Class != BerClass.Universal) { ThrowError(110, "Non-universal inner tag encountered"); } if (type == 0 || type >= BerType.LastUniversal) { ThrowError(109, "Invalid BER type encountered"); } } IsContainer = typeTag.IsContainer; Type = type; } ResetState(DecodeState.Length); return(false); } _bytesRead++; return(false); }
void Test_DOM() { Console.WriteLine("\r\n------------------------ DOM"); EmberContainer container1; EmberContainer frame = new EmberFrame(); container1 = new EmberSet(new BerTag(DefaultClass, 0)); container1.Insert(new BerTag(DefaultClass, 0), -1); container1.Insert(new BerTag(DefaultClass, 1), 128); container1.Insert(new BerTag(DefaultClass, 2), -128); container1.Insert(new BerTag(DefaultClass, 3), 255); container1.Insert(new BerTag(DefaultClass, 4), -255); container1.Insert(new BerTag(DefaultClass, 5), 12345); container1.Insert(new BerTag(DefaultClass, 6), -12345); container1.Insert(new BerTag(DefaultClass, 7), 16384); container1.Insert(new BerTag(DefaultClass, 8), -16384); container1.Insert(new BerTag(DefaultClass, 9), 65535); container1.Insert(new BerTag(DefaultClass, 10), -65535); container1.Insert(new BerTag(DefaultClass, 11), 0); container1.Insert(new BerTag(DefaultClass, 12), 127); container1.Insert(new BerTag(DefaultClass, 13), -127); container1.Insert(new BerTag(DefaultClass, 1111222), 0xFFFFFFFF); container1.InsertOid(new BerTag(DefaultClass, 14), new int[] { 1, 3, 6, 0 }); container1.InsertOid(new BerTag(DefaultClass, 15), new int[] { 1 }); container1.InsertRelativeOid(new BerTag(DefaultClass, 16), new int[] { 1, 2, 3, 4, 5, 6 }); frame.Insert(container1); container1 = new EmberSequence(new BerTag(DefaultClass, 1)); container1.Insert(new BerTag(DefaultClass, 3), -0.54321); container1.Insert(new BerTag(DefaultClass, 5), "Wuppdich"); var appDefined = EmberApplicationInterface.CreateApplicationDefinedSequence(new BerTag(BerClass.Application, 889), 2, container1); appDefined.Insert(new BerTag(DefaultClass, 100), true); frame.Insert(container1); var xml1 = GetXml(frame); var output = new BerMemoryOutput(); frame.Encode(output); var memory = output.ToArray(); using (var stream = new FileStream(@"N:\Temp\test.ber", FileMode.Create, FileAccess.Write)) stream.Write(memory, 0, memory.Length); var input = new BerMemoryInput(memory); var stopwatch = new Stopwatch(); stopwatch.Start(); var asyncReader = new AsyncFrameReader(this); asyncReader.ReadBytes(output.Memory); var loadedFrame = asyncReader.DetachRoot(); stopwatch.Stop(); Console.WriteLine("load tree: {0}ms", stopwatch.ElapsedMilliseconds); var xml2 = GetXml(loadedFrame); Console.WriteLine(xml1); Console.WriteLine(xml2); Debug.Assert(xml1 == xml2); }