Example #1
0
 public override string dump(string prefix)
 {
     if (Raw.Length > 0 && (Raw[0] == (byte)Unmarshal.ZlibMarker || Raw[0] == (byte)Unmarshal.HeaderByte))
     {
         // We have serialized python data, decode and display it.
         string pfx1 = prefix + PrettyPrinter.Spacer;
         try
         {
             Unmarshal un  = new Unmarshal();
             PyRep     obj = un.Process(Raw);
             if (obj != null)
             {
                 string sType = "<serialized>";
                 if (Raw[0] == Unmarshal.ZlibMarker)
                 {
                     sType = "<serialized-compressed>";
                 }
                 return("[PyString " + sType + Environment.NewLine + pfx1 + obj.dump(pfx1) + Environment.NewLine + prefix + "]");
             }
         }
         catch (Exception)
         {
         }
     }
     if (!PrettyPrinter.containsBinary(Raw))
     {
         return("[PyString \"" + Value + "\"]");
     }
     else
     {
         return("[PyString \"" + Value + "\"" + Environment.NewLine + prefix + "          <binary len=" + Value.Length + "> hex=\"" + PrettyPrinter.ByteArrayToString(Raw) + "\"]");
     }
 }
Example #2
0
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            int count = -1;

            switch (op)
            {
            case MarshalOpcode.ListEmpty:
                count = 0;
                break;

            case MarshalOpcode.ListOne:
                count = 1;
                break;

            case MarshalOpcode.List:
                count = (int)source.ReadSizeEx();
                break;
            }

            if (count >= 0)
            {
                Items = new List <PyRep>(count);
                for (int i = 0; i < count; i++)
                {
                    Items.Add(context.ReadObject(source));
                }
            }
        }
Example #3
0
        public override void Decode(Unmarshal context, MarshalOpcode op)
        {
            int count = -1;

            switch (op)
            {
            case MarshalOpcode.TupleEmpty:
                count = 0;
                break;

            case MarshalOpcode.TupleOne:
                count = 1;
                break;

            case MarshalOpcode.TupleTwo:
                count = 2;
                break;

            case MarshalOpcode.Tuple:
                count = (int)context.reader.ReadSizeEx();
                break;
            }

            if (count >= 0)
            {
                Items = new List <PyRep>(count);
                for (int i = 0; i < count; i++)
                {
                    Items.Add(context.ReadObject());
                }
            }
        }
Example #4
0
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            byte len = source.ReadByte();

            RawToken = source.ReadBytes(len);
            Token    = Encoding.ASCII.GetString(RawToken);
        }
Example #5
0
 public PySubStream(byte[] data)
     : base(PyObjectType.SubStream)
 {
     RawData       = data;
     DataUnmarshal = new Unmarshal();
     Data          = DataUnmarshal.Process(data);
 }
Example #6
0
        public override void Decode(Unmarshal context, MarshalOpcode op)
        {
            byte len = context.reader.ReadByte();

            RawToken = context.reader.ReadBytes(len);
            Token    = Encoding.ASCII.GetString(RawToken);
        }
Example #7
0
        public override void Decode(Unmarshal context, MarshalOpcode op)
        {
            if (op == MarshalOpcode.ObjectEx2)
            {
                IsType2 = true;
            }

            Dictionary = new Dictionary <PyRep, PyRep>();
            List       = new List <PyRep>();
            Header     = context.ReadObject();

            while (context.reader.BaseStream.Position < context.reader.BaseStream.Length)
            {
                var b = context.reader.ReadByte();
                if (b == PackedTerminator)
                {
                    break;
                }
                context.reader.BaseStream.Seek(-1, SeekOrigin.Current);
                List.Add(context.ReadObject());
            }

            while (context.reader.BaseStream.Position < context.reader.BaseStream.Length)
            {
                var b = context.reader.ReadByte();
                if (b == PackedTerminator)
                {
                    break;
                }
                context.reader.BaseStream.Seek(-1, SeekOrigin.Current);
                var key   = context.ReadObject();
                var value = context.ReadObject();
                Dictionary.Add(key, value);
            }
        }
Example #8
0
 public override void Decode(Unmarshal context, MarshalOpcode op)
 {
     if (op == MarshalOpcode.IntegerOne)
     {
         Value = 1;
     }
     else if (op == MarshalOpcode.IntegerZero)
     {
         Value = 0;
     }
     else if (op == MarshalOpcode.IntegerMinusOne)
     {
         Value = -1;
     }
     else if (op == MarshalOpcode.IntegerByte)
     {
         Value = context.reader.ReadByte();
     }
     else if (op == MarshalOpcode.IntegerSignedShort)
     {
         Value = context.reader.ReadInt16();
     }
     else if (op == MarshalOpcode.IntegerLong)
     {
         Value = context.reader.ReadInt32();
     }
 }
Example #9
0
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            uint len = source.ReadSizeEx();

            RawData       = source.ReadBytes((int)len);
            DataUnmarshal = new Unmarshal();
            Data          = DataUnmarshal.Process(RawData);
        }
Example #10
0
        public override void Decode(Unmarshal context, MarshalOpcode op)
        {
            uint len = context.reader.ReadSizeEx();

            RawData       = context.reader.ReadBytes((int)len);
            DataUnmarshal = new Unmarshal();
            DataUnmarshal.analizeInput = context.analizeInput;
            Data = DataUnmarshal.Process(RawData);
        }
Example #11
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     if (op == MarshalOpcode.RealZero)
     {
         Value = 0.0d;
     }
     else
     {
         Value = source.ReadDouble();
     }
 }
Example #12
0
 public override void Decode(Unmarshal context, MarshalOpcode op)
 {
     if (op == MarshalOpcode.RealZero)
     {
         Value = 0.0d;
     }
     else
     {
         Value = context.reader.ReadDouble();
     }
 }
Example #13
0
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            var nameObject = context.ReadObject(source);

            if (nameObject.Type != PyObjectType.String)
            {
                throw new DataException("Expected PyString");
            }
            Name = (nameObject as PyString).Value;

            Arguments = context.ReadObject(source);
        }
Example #14
0
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            var entries = source.ReadSizeEx();

            Dictionary = new Dictionary <PyRep, PyRep>((int)entries);
            for (uint i = 0; i < entries; i++)
            {
                var value = context.ReadObject(source);
                var key   = context.ReadObject(source);
                Dictionary.Add(key, value);
            }
        }
Example #15
0
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            switch (op)
            {
            case MarshalOpcode.BoolTrue:
                Value = true;
                break;

            case MarshalOpcode.BoolFalse:
                Value = false;
                break;
            }
        }
Example #16
0
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            PyRep obj = context.ReadObject(source);

            Descriptor = obj as DBRowDescriptor;
            if (Descriptor == null)
            {
                throw new InvalidDataException("PyPackedRow: Header must be DBRowDescriptor got " + obj.Type);
            }
            RawData = LoadZeroCompressed(source);

            if (!ParseRowData(context, source))
            {
                throw new InvalidDataException("Could not fully unpack PackedRow, stream integrity is broken");
            }
        }
Example #17
0
        private static byte[] LoadZeroCompressed(Unmarshal context)
        {
            var  ret       = new List <byte>();
            uint packedLen = context.reader.ReadSizeEx();
            long max       = context.reader.BaseStream.Position + packedLen;

            while (context.reader.BaseStream.Position < max)
            {
                var opcode = new ZeroCompressOpcode(context.reader.ReadByte());

                if (opcode.FirstIsZero)
                {
                    for (int n = 0; n < (opcode.FirstLength + 1); n++)
                    {
                        ret.Add(0x00);
                    }
                }
                else
                {
                    int bytes = (int)Math.Min(8 - opcode.FirstLength, max - context.reader.BaseStream.Position);
                    for (int n = 0; n < bytes; n++)
                    {
                        ret.Add(context.reader.ReadByte());
                    }
                }

                if (opcode.SecondIsZero)
                {
                    for (int n = 0; n < (opcode.SecondLength + 1); n++)
                    {
                        ret.Add(0x00);
                    }
                }
                else
                {
                    int bytes = (int)Math.Min(8 - opcode.SecondLength, max - context.reader.BaseStream.Position);
                    for (int n = 0; n < bytes; n++)
                    {
                        ret.Add(context.reader.ReadByte());
                    }
                }
            }
            return(ret.ToArray());
        }
Example #18
0
 public override void Decode(Unmarshal context, MarshalOpcode op)
 {
     decodedAnalized = context.analizeInput;
     opCode          = op;
     if (op == MarshalOpcode.StringEmpty)
     {
         Update(new byte[0]);
     }
     else if (op == MarshalOpcode.StringChar)
     {
         Update(new[] { context.reader.ReadByte() });
     }
     else if (op == MarshalOpcode.WStringUTF8)
     {
         UpdateUTF8(context.reader.ReadBytes((int)context.reader.ReadSizeEx()));
     }
     else if (op == MarshalOpcode.WStringUCS2Char)
     {
         Update(new[] { context.reader.ReadByte(), context.reader.ReadByte() }, true);
     }
     else if (op == MarshalOpcode.WStringEmpty)
     {
         Update(new byte[0]);
     }
     else if (op == MarshalOpcode.WStringUCS2)
     {
         Update(context.reader.ReadBytes((int)context.reader.ReadSizeEx() * 2), true);
     }
     else if (op == MarshalOpcode.StringShort)
     {
         Update(context.reader.ReadBytes(context.reader.ReadByte()));
     }
     else if (op == MarshalOpcode.StringLong)
     {
         Update(context.reader.ReadBytes((int)context.reader.ReadSizeEx()));
     }
     else if (op == MarshalOpcode.StringTable)
     {
         tableIndex = context.reader.ReadByte();
         Update(StringTable.Entries[tableIndex - 1]);
     }
 }
Example #19
0
        public override string dump(string prefix)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine(prefix + "[PyBuffer " + Data.Length + " bytes]" + PrettyPrinter.PrintRawData(this));
            if (Data[0] == Unmarshal.HeaderByte || Data[0] == Unmarshal.ZlibMarker)
            {
                string    pfx1 = prefix + PrettyPrinter.Spacer;
                Unmarshal un   = new Unmarshal();
                PyRep     rep  = un.Process(Data);
                if (rep != null)
                {
                    if (Data[0] == Unmarshal.ZlibMarker)
                    {
                        builder.AppendLine("<compressed-data>");
                    }
                    builder.AppendLine(pfx1 + rep.dump(pfx1));
                }
            }
            return(builder.ToString());
        }
Example #20
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     if (op == MarshalOpcode.StringEmpty)
     {
         Update(new byte[0]);
     }
     else if (op == MarshalOpcode.StringChar)
     {
         Update(new[] { source.ReadByte() });
     }
     else if (op == MarshalOpcode.WStringUTF8)
     {
         UpdateUTF8(source.ReadBytes((int)source.ReadSizeEx()));
     }
     else if (op == MarshalOpcode.WStringUCS2Char)
     {
         Update(new[] { source.ReadByte(), source.ReadByte() }, true);
     }
     else if (op == MarshalOpcode.WStringEmpty)
     {
         Update(new byte[0]);
     }
     else if (op == MarshalOpcode.WStringUCS2)
     {
         Update(source.ReadBytes((int)source.ReadSizeEx() * 2), true);
     }
     else if (op == MarshalOpcode.StringShort)
     {
         Update(source.ReadBytes(source.ReadByte()));
     }
     else if (op == MarshalOpcode.StringLong)
     {
         Update(source.ReadBytes((int)source.ReadSizeEx()));
     }
     else if (op == MarshalOpcode.StringTable)
     {
         byte index = source.ReadByte();
         Update(StringTable.Entries[index - 1]);
     }
 }
Example #21
0
 public override void dump(PrettyPrinter printer)
 {
     printer.addLine("[PyBuffer " + Data.Length + " bytes]" + PrettyPrinter.PrintRawData(this));
     if (Data != null && (Data[0] == Unmarshal.HeaderByte || Data[0] == Unmarshal.ZlibMarker || Data[0] == 0x63))
     {
         byte[] d = Data;
         if (d[0] == Unmarshal.ZlibMarker)
         {
             d = Zlib.Decompress(d);
         }
         if (d != null && (d[0] == Unmarshal.PythonMarker || d[0] == 0x63) && printer.decompilePython && d.Length > 60)
         {
             // We have a python file.
             Bytecode code = new Bytecode();
             if (code.load(d, d[0] == 0x63))
             {
                 Python.PrettyPrinter pp = new Python.PrettyPrinter();
                 pp.indentLevel = printer.indentLevel + 1;
                 pp.indent      = printer.indent;
                 code.dump(pp);
                 printer.addLine(pp.dump);
             }
         }
         else if (d != null && d[0] == Unmarshal.HeaderByte)
         {
             Unmarshal un  = new Unmarshal();
             PyRep     rep = un.Process(d);
             if (rep != null)
             {
                 if (Data[0] == Unmarshal.ZlibMarker)
                 {
                     printer.addLine("<compressed-data>");
                 }
                 printer.addItem(rep);
             }
         }
     }
 }
Example #22
0
        public override void Decode(Unmarshal context, MarshalOpcode op)
        {
            PyRep obj = context.ReadObject();

            Descriptor = obj as DBRowDescriptor;
            if (Descriptor == null)
            {
                if (!context.analizeInput && obj is PyObjectEx)
                {
                    DescriptorObj = obj;
                }
                else
                {
                    throw new InvalidDataException("PyPackedRow: Header must be DBRowDescriptor got " + obj.Type);
                }
            }
            RawData = LoadZeroCompressed(context);

            if (!ParseRowData(context))
            {
                throw new InvalidDataException("Could not fully unpack PackedRow, stream integrity is broken");
            }
        }
Example #23
0
        public override void dump(PrettyPrinter printer)
        {
            string name = "Unknown";

            if (opCode == MarshalOpcode.StringEmpty)
            {
                name = "PyStringEmpty";
            }
            else if (opCode == MarshalOpcode.StringChar)
            {
                name = "PyStringChar";
            }
            else if (opCode == MarshalOpcode.WStringUTF8)
            {
                name = "PyWStringUTF8";
            }
            else if (opCode == MarshalOpcode.WStringUCS2Char)
            {
                name = "PyWStringUCS2Char";
            }
            else if (opCode == MarshalOpcode.WStringEmpty)
            {
                name = "PyWStringEmpty";
            }
            else if (opCode == MarshalOpcode.WStringUCS2)
            {
                name = "PyWStringUCS2";
            }
            else if (opCode == MarshalOpcode.StringShort)
            {
                name = "PyString";
            }
            else if (opCode == MarshalOpcode.StringLong)
            {
                name = "PyString";
            }
            else if (opCode == MarshalOpcode.StringTable)
            {
                name = "PyStringTable[" + tableIndex.ToString() + "]";
            }

            bool printed = false;

            if (Raw.Length > 0 && (Raw[0] == (byte)Unmarshal.ZlibMarker || Raw[0] == (byte)Unmarshal.HeaderByte || Raw[0] == Unmarshal.PythonMarker || Raw[0] == 0x63))
            {
                // We have serialized python data, decode and display it.
                byte[] d = Raw;
                if (d[0] == Unmarshal.ZlibMarker)
                {
                    d = Zlib.Decompress(d);
                }
                if (d != null && (d[0] == Unmarshal.PythonMarker || d[0] == 0x63) && printer.decompilePython && d.Length > 60)
                {
                    // We have a python file.
                    Bytecode code = new Bytecode();
                    if (code.load(d, d[0] == 0x63))
                    {
                        printer.addLine("[" + name);
                        Python.PrettyPrinter pp = new Python.PrettyPrinter();
                        pp.indent      = printer.indent;
                        pp.indentLevel = printer.indentLevel + 1;
                        code.dump(pp);
                        printer.addLine(pp.dump);
                        printer.addLine("]");
                        printed = true;
                    }
                }
                else if (d != null && d[0] == Unmarshal.HeaderByte)
                {
                    Unmarshal un = new Unmarshal();
                    un.analizeInput = decodedAnalized;
                    PyRep obj = un.Process(d);
                    if (obj != null)
                    {
                        string sType = "<serialized>";
                        if (Raw[0] == Unmarshal.ZlibMarker)
                        {
                            sType = "<serialized-compressed>";
                        }
                        printer.addLine("[" + name + " " + sType);
                        printer.addItem(obj);
                        printer.addLine("]");
                        printed = true;
                    }
                }
            }
            if (!printed)
            {
                if (!PrettyPrinter.containsBinary(Raw))
                {
                    printer.addLine("[" + name + " \"" + Value + "\"]");
                }
                else
                {
                    printer.addLine("[" + name + " \"" + Value + "\"");
                    printer.indentLevel += 2;
                    printer.addLine("<binary len=" + Value.Length + "> hex=\"" + PrettyPrinter.ByteArrayToString(Raw) + "\"]");
                    printer.indentLevel -= 2;
                }
            }
        }
Example #24
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     throw new NotImplementedException();
 }
Example #25
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     throw new InvalidOperationException("Function Not Implemented.");
 }
Example #26
0
        private bool ParseRowData(Unmarshal context, BinaryReader source)
        {
            if (Descriptor == null)
            {
                return(false);
            }

            values = new PyRep[Descriptor.Columns.Count];
            var sizeList = Descriptor.Columns.OrderByDescending(c => FieldTypeHelper.GetTypeBits(c.Type));
            var sizeSum  = sizeList.Sum(c => FieldTypeHelper.GetTypeBits(c.Type));

            // align
            sizeSum = (sizeSum + 7) >> 3;
            var rawStream = new MemoryStream();

            // fill up
            rawStream.Write(RawData, 0, RawData.Length);
            for (int i = 0; i < (sizeSum - RawData.Length); i++)
            {
                rawStream.WriteByte(0);
            }
            rawStream.Seek(0, SeekOrigin.Begin);
            var reader = new BinaryReader(rawStream);

            int bitOffset = 0;

            foreach (var column in sizeList)
            {
                PyRep value = null;
                switch (column.Type)
                {
                case FieldType.I8:
                case FieldType.UI8:
                case FieldType.CY:
                case FieldType.FileTime:
                    value = new PyLongLong(reader.ReadInt64());
                    break;

                case FieldType.I4:
                case FieldType.UI4:
                    value = new PyInt(reader.ReadInt32());
                    break;

                case FieldType.I2:
                case FieldType.UI2:
                    value = new PyInt(reader.ReadInt16());
                    break;

                case FieldType.I1:
                case FieldType.UI1:
                    value = new PyInt(reader.ReadByte());
                    break;

                case FieldType.R8:
                    value = new PyFloat(reader.ReadDouble());
                    break;

                case FieldType.R4:
                    value = new PyFloat(reader.ReadSingle());
                    break;

                case FieldType.Bytes:
                case FieldType.Str:
                case FieldType.WStr:
                    value = context.ReadObject(source);
                    break;

                case FieldType.Bool:
                {
                    if (7 < bitOffset)
                    {
                        bitOffset = 0;
                        reader.ReadByte();
                    }

                    var b = reader.ReadByte();
                    reader.BaseStream.Seek(-1, SeekOrigin.Current);
                    value = new PyInt((b >> bitOffset++) & 0x01);
                    break;
                }

                case FieldType.Token:
                    value = new PyToken(column.Token);
                    break;

                default:
                    throw new Exception("No support for " + column.Type);
                }
                int index = Descriptor.Columns.FindIndex(x => x.Name == column.Name);
                values[index] = value;
            }

            return(true);
        }
Example #27
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     Checksum = source.ReadUInt32();
     Data     = context.ReadObject(source);
 }
Example #28
0
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            var len = source.ReadSizeEx();

            Raw = source.ReadBytes((int)len);
        }
Example #29
0
 public override void Decode(Unmarshal context, MarshalOpcode op)
 {
     throw new NotImplementedException();
 }
Example #30
0
 public override void Decode(Unmarshal context, MarshalOpcode op)
 {
     Checksum = context.reader.ReadUInt32();
     Data     = context.ReadObject();
 }