Beispiel #1
0
            public override StackItem ToStackItem(ReferenceCounter referenceCounter)
            {
                Struct @struct = (Struct)base.ToStackItem(referenceCounter);

                @struct.Add(Expiration);
                @struct.Add(Admin?.ToArray() ?? StackItem.Null);
                return(@struct);
            }
Beispiel #2
0
            protected override Struct ToStruct()
            {
                Struct @struct = base.ToStruct();

                @struct.Add(BalanceHeight);
                @struct.Add(Votes.ToByteArray());
                return(@struct);
            }
Beispiel #3
0
            public override StackItem ToStackItem(ReferenceCounter referenceCounter)
            {
                Struct @struct = (Struct)base.ToStackItem(referenceCounter);

                @struct.Add(BalanceHeight);
                @struct.Add(VoteTo?.ToArray() ?? StackItem.Null);
                return(@struct);
            }
        public override StackItem ToStackItem(ReferenceCounter referenceCounter)
        {
            Struct @struct = (Struct)base.ToStackItem(referenceCounter);

            @struct.Add((byte)ReturnType);
            @struct.Add(Offset);
            @struct.Add(Safe);
            return(@struct);
        }
Beispiel #5
0
            public override StackItem ToStackItem(ReferenceCounter referenceCounter)
            {
                Struct @struct = (Struct)base.ToStackItem(referenceCounter);

                @struct.Add(new Array(referenceCounter, Tokens.Select(p => (StackItem)p)));
                return(@struct);
            }
Beispiel #6
0
        /// <summary>
        /// Create a struct from a dynamic object.
        /// </summary>
        /// <param name="obj"></param>
        public XmlRpcStruct(DynamicObject obj) : base(null)
        {
            Fields = new Struct();

            foreach (var kv in obj)
            {
                Fields.Add(kv.Key, XmlRpcTypes.ToXmlRpcValue(obj));
            }
        }
Beispiel #7
0
        /// <summary>
        /// Create a struct from an object.
        /// </summary>
        /// <param name="obj"></param>
        public XmlRpcStruct(object obj) : base(null)
        {
            Type t      = obj.GetType();
            var  fields = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            Fields = new Struct();

            foreach (var field in fields)
            {
                Fields.Add(field.Name, XmlRpcTypes.ToXmlRpcValue(t.GetProperty(field.Name).GetValue(obj)));
            }
        }
Beispiel #8
0
        public static StackItem StackItemFromJson(JObject json)
        {
            StackItemType type = json["type"].TryGetEnum <StackItemType>();

            switch (type)
            {
            case StackItemType.Boolean:
                return(new Boolean(json["value"].AsBoolean()));

            case StackItemType.Buffer:
                return(new Buffer(Convert.FromBase64String(json["value"].AsString())));

            case StackItemType.ByteString:
                return(new ByteString(Convert.FromBase64String(json["value"].AsString())));

            case StackItemType.Integer:
                return(new Integer(new BigInteger(json["value"].AsNumber())));

            case StackItemType.Array:
                Array array = new Array();
                foreach (var item in (JArray)json["value"])
                {
                    array.Add(StackItemFromJson(item));
                }
                return(array);

            case StackItemType.Struct:
                Struct @struct = new Struct();
                foreach (var item in (JArray)json["value"])
                {
                    @struct.Add(StackItemFromJson(item));
                }
                return(@struct);

            case StackItemType.Map:
                Map map = new Map();
                foreach (var item in (JArray)json["value"])
                {
                    PrimitiveType key = (PrimitiveType)StackItemFromJson(item["key"]);
                    map[key] = StackItemFromJson(item["value"]);
                }
                return(map);

            case StackItemType.Pointer:
                return(new Pointer(null, (int)json["value"].AsNumber()));

            case StackItemType.InteropInterface:
                return(new InteropInterface(new object()));    // See https://github.com/neo-project/neo/blob/master/src/neo/VM/Helper.cs#L194
            }
            return(json["value"] is null ? StackItem.Null : json["value"].AsString());
        }
Beispiel #9
0
        public XmlRpcStruct(XElement element) : base(element)
        {
            Fields = new Struct();
            var members = element.Elements(XmlRpcElementNames.Member);

            foreach (XElement member in members)
            {
                string   name         = member.Elements(XmlRpcElementNames.Name).First().Value;
                XElement valueElement = member.Elements(XmlRpcElementNames.Value)
                                        .First()
                                        .Elements()
                                        .First();

                XmlRpcBaseType value = XmlRpcTypes.ElementToInstance(valueElement);

                Fields.Add(name, value);
            }
        }
Beispiel #10
0
        /// <summary>
        /// 创建结构体
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <param name="address"></param>
        /// <param name="canPostfix"></param>
        /// <returns></returns>
        public static Struct Create <TEntity>(TEntity entity, UInt32 address, Boolean canPostfix) where TEntity : EntityBase <TEntity>, new()
        {
            Type type = typeof(TEntity);

            // 类名作为结构体名字
            String name = type.Name;

            // 检查是否已存在
            //UInt32 id = Struct.GetStructID(name);
            //if (id != Bytes.BadAddress)
            //{
            //    KernelWin.WriteLine("结构体{0}(ID={1:X}h)已存在!", name, id);
            //    return Struct.FindStructByID(id);
            //}

            Struct st = Struct.FindStructByName(name);
            //if (st != null)
            //{
            //    KernelWin.WriteLine("结构体{0}(ID={1:X}h)已存在!", name, st.ID);
            //    return st;
            //}

            Int32  n     = 0;
            String name2 = name;

            while (st == null)
            {
                st = Struct.Create(name2, Bytes.BadAddress, false);

                n++;
                if (st == null)
                {
                    if (n > 10 || !canPostfix)
                    {
                        throw new Exception("创建结构体失败!");
                    }
                    name2 = name + "_" + n;
                }
            }

            Dictionary <String, DataFieldItem> dic = DataFieldItem.GetFields(type);

            foreach (DataFieldItem item in dic.Values)
            {
                name = item.Property.Name;
                DataType dt;
                UInt32   size = 0;

                if (item.Attribute.RefType == null)
                {
                    #region 普通成员
                    if (item.Property.PropertyType == typeof(String))
                    {
                        dt = DataType.ASCI;
                        if (!String.IsNullOrEmpty(item.Attribute.SizeField))
                        {
                            size = Convert.ToUInt32(entity[item.Attribute.SizeField]);
                        }
                        if (size <= 0)
                        {
                            size = (UInt32)item.Attribute.Size;
                        }
                    }
                    else if (item.Property.PropertyType == typeof(Byte[]))
                    {
                        // 字节数组也可以当作字符串一样处理
                        dt = DataType.ASCI;
                        if (!String.IsNullOrEmpty(item.Attribute.SizeField))
                        {
                            size = Convert.ToUInt32(entity[item.Attribute.SizeField]);
                        }
                        if (size <= 0)
                        {
                            size = (UInt32)item.Attribute.Size;
                        }
                    }
                    else if (item.Property.PropertyType == typeof(Byte))
                    {
                        dt   = DataType.BYTE;
                        size = 1;
                    }
                    else if (item.Property.PropertyType == typeof(Int16))
                    {
                        dt   = DataType.WORD;
                        size = 2;
                    }
                    else if (item.Property.PropertyType == typeof(Int32))
                    {
                        dt   = DataType.DWRD;
                        size = 4;
                    }
                    else
                    {
                        throw new Exception("不支持的类型:" + type.Name);
                    }

                    //KernelWin.WriteLine("创建普通成员:{0} {1} {2} ", name, dt, size);
                    st.Add(name, dt, size);

                    #endregion
                }
                else
                {
                    #region 引用成员
                    if (item.Property.PropertyType == typeof(Int16))
                    {
                        dt   = DataType.WORD | DataType.F0OFF;
                        size = 2;
                    }
                    else if (item.Property.PropertyType == typeof(Int32))
                    {
                        dt   = DataType.DWRD | DataType.F0OFF;
                        size = 4;
                    }
                    else
                    {
                        throw new Exception("不支持的类型:" + type.Name);
                    }

                    UInt32 addr = 0;
                    switch (item.Attribute.RefKind)
                    {
                    case RefKinds.Virtual:
                        addr = 0;
                        break;

                    case RefKinds.Relative:
                        addr = address;
                        break;

                    case RefKinds.Auto:
                        // 先直接取地址
                        addr = Convert.ToUInt32(item.Property.GetValue(entity, null));
                        // 如果小于基址,可能是相对地址
                        if (addr < entity.Info.ImageBase && addr > 0)
                        {
                            addr = address;
                        }
                        else
                        {
                            addr = 0;
                        }
                        break;

                    case RefKinds.Absolute:
                        throw new Exception("不支持的类型:" + item.Attribute.RefKind);
                    }

                    //KernelWin.WriteLine("创建引用成员:{0} {1} 0x{2:X} {3}", name, dt, addr, size);
                    st.Add(name, Bytes.BadAddress, dt, addr, size);

                    #endregion
                }
            }

            return(st);
        }
Beispiel #11
0
        private StackItem DeserializeStackItem(BinaryReader reader, ExecutionEngine engine, uint maxItemSize)
        {
            Stack <StackItem> deserialized = new Stack <StackItem>();
            int undeserialized             = 1;

            while (undeserialized-- > 0)
            {
                StackItemType type = (StackItemType)reader.ReadByte();
                switch (type)
                {
                case StackItemType.ByteArray:
                    deserialized.Push(new ByteArray(reader.ReadVarBytes((int)maxItemSize)));
                    break;

                case StackItemType.Boolean:
                    deserialized.Push(new VMBoolean(reader.ReadBoolean()));
                    break;

                case StackItemType.Integer:
                    deserialized.Push(new Integer(new BigInteger(reader.ReadVarBytes(ExecutionEngine.MaxSizeForBigInteger))));
                    break;

                case StackItemType.Array:
                case StackItemType.Struct:
                {
                    int count = (int)reader.ReadVarInt(engine.MaxArraySize);
                    deserialized.Push(new ContainerPlaceholder
                        {
                            Type         = type,
                            ElementCount = count
                        });
                    undeserialized += count;
                }
                break;

                case StackItemType.Map:
                {
                    int count = (int)reader.ReadVarInt(engine.MaxArraySize);
                    deserialized.Push(new ContainerPlaceholder
                        {
                            Type         = type,
                            ElementCount = count
                        });
                    undeserialized += count * 2;
                }
                break;

                default:
                    throw new FormatException();
                }
            }
            Stack <StackItem> stack_temp = new Stack <StackItem>();

            while (deserialized.Count > 0)
            {
                StackItem item = deserialized.Pop();
                if (item is ContainerPlaceholder placeholder)
                {
                    switch (placeholder.Type)
                    {
                    case StackItemType.Array:
                        VMArray array = new VMArray();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            array.Add(stack_temp.Pop());
                        }
                        item = array;
                        break;

                    case StackItemType.Struct:
                        Struct @struct = new Struct();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            @struct.Add(stack_temp.Pop());
                        }
                        item = @struct;
                        break;

                    case StackItemType.Map:
                        Map map = new Map();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            StackItem key   = stack_temp.Pop();
                            StackItem value = stack_temp.Pop();
                            map.Add(key, value);
                        }
                        item = map;
                        break;
                    }
                }
                stack_temp.Push(item);
            }
            return(stack_temp.Peek());
        }