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!"); }
/// <summary> /// Dispose of allocated resources. /// </summary> /// <param name="disposing">If true, dispose of unmanaged resources /// otherwise only dispose of managed resources</param> protected virtual void Dispose(bool disposing) { _buffer = null; }
/// <summary> /// Writes a byte of ember data to the output. /// </summary> /// <param name="b">Byte to write</param> public void WriteByte(byte b) { if(_buffer == null) _buffer = new BerMemoryOutput(); if(_buffer.Length >= MaximumPackageLength) OnPackageReady(isLastPackage: false); if(_dataLength == 0) WriteHeader(); _buffer.WriteByte(b); _dataLength++; }
public static byte[] Encode(string format, params object[] args) { var output = new BerMemoryOutput(); var paramIndex = 0; for(int charIndex = 0; charIndex < format.Length; charIndex++) { var ch = format[charIndex]; switch(ch) { case '{': EncodeHeader(output, TypeTags.Sequence, BerDefinitions.IndefiniteLength); break; case '}': output.WriteByte(0); output.WriteByte(0); break; case 'b': { EncodeHeader(output, TypeTags.Boolean, 1); BerEncoding.EncodeBoolean(output, (bool)args[paramIndex++]); break; } case 'i': case 'd': { var value = (int)args[paramIndex++]; var valueLength = BerEncoding.GetIntegerLength(value); EncodeHeader(output, TypeTags.Integer, valueLength); BerEncoding.EncodeInteger(output, value, (int)valueLength); break; } case 'l': case 't': { var value = (long)args[paramIndex++]; var valueLength = BerEncoding.GetLongLength(value); EncodeHeader(output, TypeTags.Integer, valueLength); BerEncoding.EncodeLong(output, value, (int)valueLength); break; } case 's': { var value = (string)args[paramIndex++]; var valueLength = BerEncoding.GetUtf8StringLength(value); EncodeHeader(output, TypeTags.UTF8String, valueLength); BerEncoding.EncodeUtf8String(output, value); break; } case 'y': { var value = (byte[])args[paramIndex++]; var valueLength = value.Length; EncodeHeader(output, TypeTags.OctetString, valueLength); BerEncoding.EncodeByteArray(output, value); break; } case 'f': case 'r': { var value = (double)args[paramIndex++]; var local = new BerMemoryOutput(); var valueLength = BerEncoding.EncodeReal(local, value); EncodeHeader(output, TypeTags.Real, valueLength); output.WriteBytes(local.Memory); break; } case 'o': { var value = (int[])args[paramIndex++]; var local = new BerMemoryOutput(); var valueLength = BerEncoding.EncodeRelativeOid(local, value); EncodeHeader(output, TypeTags.RelativeOid, valueLength); output.WriteBytes(local.Memory); break; } case 'g': { var value = (DateTime)args[paramIndex++]; var local = new BerMemoryOutput(); var valueLength = BerEncoding.EncodeGeneralizedTime(local, value); EncodeHeader(output, TypeTags.GeneralizedTime, valueLength); output.WriteBytes(local.Memory); break; } } } return output.ToArray(); }
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 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_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); }
void Test_ReaderWriter() { var output = new BerMemoryOutput(); var writer = new EmberWriter(output); writer.WriteSequenceBegin(new BerTag(DefaultClass, 1)); for(uint index = 0; index <= 20; index++) writer.Write(new BerTag(DefaultClass, index + 111122), index); var oid = new int[100]; for(int index = 0; index < oid.Length; index++) oid[index] = 1000 + index; writer.WriteRelativeOid(new BerTag(DefaultClass, 500000), oid); writer.WriteContainerEnd(); Console.WriteLine("\r\n------------------------ Reader, Writer"); var asyncReader = new AsyncDomReader(null); asyncReader.ReadBytes(output.Memory); var root = asyncReader.DetachRoot(); Console.WriteLine(GetXml(root)); }
public void Write(BerTag tag, string value) { var valueOutput = new BerMemoryOutput(); var valueLength = BerEncoding.EncodeUtf8String(valueOutput, value); var innerTag = new BerTag(BerType.UTF8String); WriteOuterHeader(tag, valueLength + BerEncoding.GetHeaderLength(innerTag, valueLength)); BerEncoding.EncodeTag(_output, innerTag); BerEncoding.EncodeLength(_output, valueLength); _output.WriteBytes(valueOutput.Memory); }
public void Write(BerTag tag, double value) { var valueOutput = new BerMemoryOutput(); var valueLength = BerEncoding.EncodeReal(valueOutput, value); WriteOuterHeader(tag, valueLength + 2); BerEncoding.EncodeTag(_output, new BerTag(BerType.Real)); BerEncoding.EncodeLength(_output, valueLength); _output.WriteBytes(valueOutput.Memory); }
public static byte[] Encode(string format, params object[] args) { var output = new BerMemoryOutput(); var paramIndex = 0; for (int charIndex = 0; charIndex < format.Length; charIndex++) { var ch = format[charIndex]; switch (ch) { case '{': EncodeHeader(output, TypeTags.Sequence, BerDefinitions.IndefiniteLength); break; case '}': output.WriteByte(0); output.WriteByte(0); break; case 'b': { EncodeHeader(output, TypeTags.Boolean, 1); BerEncoding.EncodeBoolean(output, (bool)args[paramIndex++]); break; } case 'i': case 'd': { var value = (int)args[paramIndex++]; var valueLength = BerEncoding.GetIntegerLength(value); EncodeHeader(output, TypeTags.Integer, valueLength); BerEncoding.EncodeInteger(output, value, (int)valueLength); break; } case 'l': case 't': { var value = (long)args[paramIndex++]; var valueLength = BerEncoding.GetLongLength(value); EncodeHeader(output, TypeTags.Integer, valueLength); BerEncoding.EncodeLong(output, value, (int)valueLength); break; } case 's': { var value = (string)args[paramIndex++]; var valueLength = BerEncoding.GetUtf8StringLength(value); EncodeHeader(output, TypeTags.UTF8String, valueLength); BerEncoding.EncodeUtf8String(output, value); break; } case 'y': { var value = (byte[])args[paramIndex++]; var valueLength = value.Length; EncodeHeader(output, TypeTags.OctetString, valueLength); BerEncoding.EncodeByteArray(output, value); break; } case 'f': case 'r': { var value = (double)args[paramIndex++]; var local = new BerMemoryOutput(); var valueLength = BerEncoding.EncodeReal(local, value); EncodeHeader(output, TypeTags.Real, valueLength); output.WriteBytes(local.Memory); break; } case 'o': { var value = (int[])args[paramIndex++]; var local = new BerMemoryOutput(); var valueLength = BerEncoding.EncodeRelativeOid(local, value); EncodeHeader(output, TypeTags.RelativeOid, valueLength); output.WriteBytes(local.Memory); break; } case 'g': { var value = (DateTime)args[paramIndex++]; var local = new BerMemoryOutput(); var valueLength = BerEncoding.EncodeGeneralizedTime(local, value); EncodeHeader(output, TypeTags.GeneralizedTime, valueLength); output.WriteBytes(local.Memory); break; } } } return(output.ToArray()); }
void Test_ReaderWriter() { var output = new BerMemoryOutput(); var writer = new EmberWriter(output); writer.WriteFrameBegin(); writer.WriteSequenceBegin(new BerTag(DefaultClass, 1)); for(uint index = 0; index <= 100; index++) writer.Write(new BerTag(DefaultClass, index + 111122), index); writer.WriteContainerEnd(); writer.WriteContainerEnd(); Console.WriteLine("\r\n------------------------ Reader, Writer"); var asyncReader = new AsyncDomReader(null); asyncReader.ReadBytes(output.Memory); DumpXml(asyncReader.DetachRoot()); //var input = new BerMemoryInput(output.Memory); //var reader = new EmberReader(input); //var xmlBuffer = new StringBuilder(); //var xmlSettings = new XmlWriterSettings //{ // OmitXmlDeclaration = true, //}; //using(var xmlWriter = XmlWriter.Create(xmlBuffer, xmlSettings)) // EmberToXml(reader, xmlWriter); //Console.WriteLine(xmlBuffer.ToString()); }
internal override int Update() { var output = new BerMemoryOutput(); var implicitTag = new BerTag(BerClass.Universal, BerTypeNumber, true); var childrenLength = 0; foreach(var child in this) { childrenLength += child.IsDirty ? child.Update() : child.EncodedLength; } BerEncoding.EncodeTag(output, Tag.ToContainer()); BerEncoding.EncodeLength(output, BerDefinitions.IndefiniteLength); BerEncoding.EncodeTag(output, implicitTag); BerEncoding.EncodeLength(output, BerDefinitions.IndefiniteLength); _encodedFrameHeader = output.ToArray(); EncodedLength = childrenLength + _encodedFrameHeader.Length + BerEncoding.IndefiniteLengthTerminator.Length; return EncodedLength; }