Example #1
0
        protected sealed override void Deserialize(Stream stream)
        {
            var scriptSize = (uint)ReadVarInt(stream);

            if (scriptSize > Script.MaximumScriptByteSize)
            {
                throw new SerializationException("Unable to deserialize: Script length greater than maximum allowable script size: " + scriptSize);
            }

            var atoms = new ArrayList <IScriptAtom>();
            int read  = 0;

            while (read < scriptSize)
            {
                var atom = ScriptAtomFactory.GetAtom(stream);
                read += atom.SerializedByteSize;
                atoms.Add(atom);
            }
            if (read > scriptSize)
            {
                stream.Position -= read - scriptSize;
                throw new SerializationException("Unable to deserialize: Script longer than expected.");
            }
            atoms.CollectionChanged += AtomsChanged;
            Atoms = atoms;

            Freeze();
        }
Example #2
0
        public static Script Parse(string scriptString)
        {
            var           atoms    = scriptString.Split(' ');
            var           script   = new Script();
            StringBuilder valueStr = null;

            foreach (var atom in atoms)
            {
                IScriptAtom newAtom = null;
                if (valueStr != null)
                {
                    valueStr.Append(" ");
                    if (atom.EndsWith("'"))
                    {
                        if (atom.EndsWith(@"\''"))
                        {
                            valueStr.Append(atom.Substring(0, atom.Length - 3));
                            valueStr.Append("'");
                            var valueBytes = Encoding.ASCII.GetBytes(valueStr.ToString());
                            newAtom = new ValueAtom(valueBytes);
                            script.Atoms.Add(newAtom);
                            valueStr = null;
                        }
                        else if (atom.EndsWith(@"\'"))
                        {
                            valueStr.Append(atom.Substring(0, atom.Length - 2));
                            valueStr.Append("'");
                        }
                        else
                        {
                            valueStr.Append(atom.Substring(0, atom.Length - 1));
                            var valueBytes = Encoding.ASCII.GetBytes(valueStr.ToString());
                            newAtom = new ValueAtom(valueBytes);
                            script.Atoms.Add(newAtom);
                            valueStr = null;
                        }
                    }
                    else
                    {
                        valueStr.Append(atom);
                    }
                    continue;
                }
                if (atom.StartsWith("OP_"))
                {
                    var opcode = (ScriptOpCode)Enum.Parse(typeof(ScriptOpCode), atom, false);
                    newAtom = ScriptAtomFactory.GetOpAtom(opcode);
                }
                else if (atom.StartsWith("'"))
                {
                    if (atom.EndsWith("'"))
                    {
                        if (atom.EndsWith(@"\''"))
                        {
                            var valueBytes = Encoding.ASCII.GetBytes(atom.Substring(1, atom.Length - 4) + "'");
                            newAtom = new ValueAtom(valueBytes);
                        }
                        else if (atom.EndsWith(@"\'"))
                        {
                            valueStr = new StringBuilder();
                            valueStr.Append(atom.Substring(1, atom.Length - 3));
                            valueStr.Append("'");
                        }
                        else if (atom.Length > 1)
                        {
                            if (atom.Length > 2)
                            {
                                var valueBytes = Encoding.ASCII.GetBytes(atom.Substring(1, atom.Length - 2));
                                newAtom = new ValueAtom(valueBytes);
                            }
                        }
                        else
                        {
                            valueStr = new StringBuilder();
                        }
                    }
                    else
                    {
                        valueStr = new StringBuilder();
                        valueStr.Append(atom.Substring(1, atom.Length - 1));
                    }
                }
                else if (atom.Length > 0)
                {
                    var valueBytes = BufferOperations.FromByteString(atom, Endianness.BigEndian);
                    newAtom = new ValueAtom(valueBytes);
                }
                if (newAtom != null)
                {
                    script.Atoms.Add(newAtom);
                }
            }
            if (valueStr != null)
            {
                throw new FormatException("Unclosed script string found");
            }
            script.Freeze();
            return(script);
        }