Esempio n. 1
0
        private uint ComputeLength(DarksVMRuntime rt)
        {
            uint len = 16;

            foreach (var reference in rt.Descriptor.Data.refMap)
            {
                len += Utils.GetCompressedUIntLength(reference.Value) + GetCodedLen(reference.Key.MDToken);
            }
            foreach (var str in rt.Descriptor.Data.strMap)
            {
                len += Utils.GetCompressedUIntLength(str.Value);
                len += Utils.GetCompressedUIntLength((uint)str.Key.Length);
                len += (uint)str.Key.Length * 2;
            }
            foreach (var sig in rt.Descriptor.Data.sigs)
            {
                len += Utils.GetCompressedUIntLength(sig.Id);
                len += 4;
                if (sig.Method != null)
                {
                    len += 4;
                }
                var paramCount = (uint)sig.FuncSig.ParamSigs.Length;
                len += 1 + Utils.GetCompressedUIntLength(paramCount);

                foreach (var param in sig.FuncSig.ParamSigs)
                {
                    len += GetCodedLen(param.MDToken);
                }
                len += GetCodedLen(sig.FuncSig.RetType.MDToken);
            }
            return(len);
        }
 public BasicBlockChunk(DarksVMRuntime rt, MethodDef method, ILBlock block)
 {
     this.rt     = rt;
     this.method = method;
     Block       = block;
     Length      = rt.serializer.ComputeLength(block);
 }
Esempio n. 3
0
        private uint ComputeLength(DarksVMRuntime rt)
        {
            uint len = 16;

            foreach (System.Collections.Generic.KeyValuePair <IMemberRef, uint> reference in rt.Descriptor.Data.refMap)
            {
                len += Utils.GetCompressedUIntLength(reference.Value) + this.GetCodedLen(reference.Key.MDToken);
            }
            foreach (System.Collections.Generic.KeyValuePair <string, uint> str in rt.Descriptor.Data.strMap)
            {
                len += Utils.GetCompressedUIntLength(str.Value);
                len += Utils.GetCompressedUIntLength((uint)str.Key.Length);
                len += (uint)str.Key.Length * 2;
            }
            foreach (VM.DataDescriptor.FuncSigDesc sig in rt.Descriptor.Data.sigs)
            {
                len += Utils.GetCompressedUIntLength(sig.Id);
                len += 4;
                if (sig.Method != null)
                {
                    len += 4;
                }
                uint paramCount = (uint)sig.FuncSig.ParamSigs.Length;
                len += 1 + Utils.GetCompressedUIntLength(paramCount);

                foreach (ITypeDefOrRef param in sig.FuncSig.ParamSigs)
                {
                    len += this.GetCodedLen(param.MDToken);
                }
                len += this.GetCodedLen(sig.FuncSig.RetType.MDToken);
            }
            return(len);
        }
Esempio n. 4
0
        internal void WriteData(DarksVMRuntime rt)
        {
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);

            writer.Write((uint)0x68736966);
            writer.Write(rt.Descriptor.Data.refMap.Count);
            writer.Write(rt.Descriptor.Data.strMap.Count);
            writer.Write(rt.Descriptor.Data.sigs.Count);

            foreach (var refer in rt.Descriptor.Data.refMap)
            {
                writer.WriteCompressedUInt(refer.Value);
                writer.WriteCompressedUInt(GetCodedToken(refer.Key.MDToken));
            }

            foreach (var str in rt.Descriptor.Data.strMap)
            {
                writer.WriteCompressedUInt(str.Value);
                writer.WriteCompressedUInt((uint)str.Key.Length);
                foreach (var chr in str.Key)
                {
                    writer.Write((ushort)chr);
                }
            }

            foreach (var sig in rt.Descriptor.Data.sigs)
            {
                writer.WriteCompressedUInt(sig.Id);
                if (sig.Method != null)
                {
                    var entry       = rt.methodMap[sig.Method].Item2;
                    var entryOffset = entry.Content[0].Offset;
                    Debug.Assert(entryOffset != 0);
                    writer.Write(entryOffset);

                    var key = (uint)rt.Descriptor.Random.Next();
                    key = (key << 8) | rt.Descriptor.Data.LookupInfo(sig.Method).EntryKey;
                    writer.Write(key);
                }
                else
                {
                    writer.Write(0u);
                }

                writer.Write(sig.FuncSig.Flags);
                writer.WriteCompressedUInt((uint)sig.FuncSig.ParamSigs.Length);
                foreach (var paramType in sig.FuncSig.ParamSigs)
                {
                    writer.WriteCompressedUInt(GetCodedToken(paramType.MDToken));
                }
                writer.WriteCompressedUInt(GetCodedToken(sig.FuncSig.RetType.MDToken));
            }

            data = stream.ToArray();
            Debug.Assert(data.Length == Length);
        }
Esempio n. 5
0
        internal void WriteData(DarksVMRuntime rt)
        {
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);

            writer.Write((uint)0x68736966);
            writer.Write(rt.Descriptor.Data.refMap.Count);
            writer.Write(rt.Descriptor.Data.strMap.Count);
            writer.Write(rt.Descriptor.Data.sigs.Count);

            foreach (System.Collections.Generic.KeyValuePair <IMemberRef, uint> refer in rt.Descriptor.Data.refMap)
            {
                writer.WriteCompressedUInt(refer.Value);
                writer.WriteCompressedUInt(this.GetCodedToken(refer.Key.MDToken));
            }

            foreach (System.Collections.Generic.KeyValuePair <string, uint> str in rt.Descriptor.Data.strMap)
            {
                writer.WriteCompressedUInt(str.Value);
                writer.WriteCompressedUInt((uint)str.Key.Length);
                foreach (char chr in str.Key)
                {
                    writer.Write((ushort)chr);
                }
            }

            foreach (VM.DataDescriptor.FuncSigDesc sig in rt.Descriptor.Data.sigs)
            {
                writer.WriteCompressedUInt(sig.Id);
                if (sig.Method != null)
                {
                    AST.IL.ILBlock entry       = rt.methodMap[sig.Method].Item2;
                    uint           entryOffset = entry.Content[0].Offset;
                    Debug.Assert(entryOffset != 0);
                    writer.Write(entryOffset);

                    uint key = (uint)rt.Descriptor.Random.Next();
                    key = (key << 8) | rt.Descriptor.Data.LookupInfo(sig.Method).EntryKey;
                    writer.Write(key);
                }
                else
                {
                    writer.Write(0u);
                }

                writer.Write(sig.FuncSig.Flags);
                writer.WriteCompressedUInt((uint)sig.FuncSig.ParamSigs.Length);
                foreach (ITypeDefOrRef paramType in sig.FuncSig.ParamSigs)
                {
                    writer.WriteCompressedUInt(this.GetCodedToken(paramType.MDToken));
                }
                writer.WriteCompressedUInt(this.GetCodedToken(sig.FuncSig.RetType.MDToken));
            }

            this.data = stream.ToArray();
            Debug.Assert(this.data.Length == this.Length);
        }
Esempio n. 6
0
 public BasicBlockSerializer(DarksVMRuntime rt)
 {
     this.rt = rt;
 }
Esempio n. 7
0
 public HeaderChunk(DarksVMRuntime rt)
 {
     Length = ComputeLength(rt);
 }
Esempio n. 8
0
 public HeaderChunk(DarksVMRuntime rt) => this.Length = this.ComputeLength(rt);