Example #1
0
        public static string Print(PyRep obj, int indention = 0)
        {
            var ret = new StringBuilder();

            Print(ret, "", obj);
            return(ret.ToString());
        }
Example #2
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 #3
0
 public PyObjectEx(bool isType2, PyRep header)
     : base(PyObjectType.ObjectEx)
 {
     IsType2    = isType2;
     Header     = header;
     Dictionary = new Dictionary <PyRep, PyRep>();
 }
Example #4
0
 public override void dump(PrettyPrinter printer)
 {
     printer.addLine("[PyPackedRow " + RawData.Length + " bytes]");
     if (Descriptor != null)
     {
         printer.indentLevel++;
         if (Descriptor.Columns != null)
         {
             foreach (var column in Descriptor.Columns)
             {
                 int   index = Descriptor.Columns.FindIndex(x => x.Name == column.Name);
                 PyRep value = values[index];
                 printer.addLine("[\"" + column.Name + "\" => " + " [" + column.Type + "] " + value + "]");
             }
         }
         else
         {
             printer.addLine("[Columns parsing failed!]");
         }
         printer.indentLevel--;
     }
     else
     {
         if (DescriptorObj != null)
         {
             printer.addItem(DescriptorObj);
         }
         else
         {
             printer.indentLevel++;
             printer.addLine("Error.. Obj missing.");
             printer.indentLevel--;
         }
     }
 }
Example #5
0
 public static string PrintRawData(PyRep obj)
 {
     if (obj.RawSource == null)
     {
         return("");
     }
     return(" [" + BitConverter.ToString(obj.RawSource, 0, obj.RawSource.Length > 8 ? 8 : obj.RawSource.Length) + "]");
 }
Example #6
0
 public PyRep this[PyRep key]
 {
     get
     {
         return(Dictionary[key]);
     }
     set
     {
         Dictionary[key] = value;
     }
 }
Example #7
0
        public static void Print(StringBuilder builder, string prefix, PyRep obj)
        {
            if (obj == null)
            {
                builder.AppendLine(prefix + "<nullptr>");
                return;
            }
            string pfx1 = prefix + Spacer;
            string pfx2 = pfx1 + Spacer;

            builder.AppendLine(prefix + obj.dump(prefix).TrimEnd('\r', '\n'));// + PrintRawData(obj));
        }
Example #8
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 #9
0
 public void addItem(PyRep rep)
 {
     indentLevel++;
     if (rep == null)
     {
         addLine("<nullptr>");
     }
     else
     {
         rep.dump(this);
     }
     indentLevel--;
 }
Example #10
0
        public static byte[] Process(PyRep obj)
        {
            var ret = new MemoryStream(100);

            ret.WriteByte(Unmarshal.HeaderByte);
            // we have no support for save lists right now
            ret.WriteByte(0x00);
            ret.WriteByte(0x00);
            ret.WriteByte(0x00);
            ret.WriteByte(0x00);
            obj.Encode(new BinaryWriter(ret));
            return(ret.ToArray());
        }
Example #11
0
 public string Print(PyRep obj)
 {
     builder = new StringBuilder();
     if (obj == null)
     {
         addLine("<nullptr>");
     }
     else
     {
         obj.dump(this);
     }
     return(builder.ToString());
 }
Example #12
0
        public void Set(string key, PyRep value)
        {
            var keyObject = Dictionary.Count > 0 ? Dictionary.Keys.Where(k => k.Type == PyObjectType.String && (k as PyString).Value == key).FirstOrDefault() : null;

            if (keyObject != null)
            {
                Dictionary[keyObject] = value;
            }
            else
            {
                Dictionary.Add(new PyString(key), value);
            }
        }
Example #13
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 #14
0
 private PyRep analyse(PyRep obj)
 {
     try
     {
         if (obj is PyObjectEx)
         {
             bool       usedList, usedDict;
             PyRep      res = obj;
             PyObjectEx ex  = obj as PyObjectEx;
             if (!ex.IsType2)
             {
                 res = analyseType1(ex, out usedList, out usedDict);
             }
             else
             {
                 res = analyseType2(ex, out usedList, out usedDict);
             }
             if (res != obj)
             {
                 if (!usedList)
                 {
                     if (ex.List != null && ex.List.Count > 0)
                     {
                         unknown.AppendLine("Unused List item in " + res.GetType());
                     }
                 }
                 if (!usedDict)
                 {
                     if (ex.Dictionary != null && ex.Dictionary.Count > 0)
                     {
                         unknown.AppendLine("Unused dictionary item in " + res.GetType());
                     }
                 }
             }
             return(res);
         }
     }
     catch (InvalidDataException)
     {
         return(obj);
     }
     return(obj);
 }
Example #15
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 #16
0
        public override string dump(string prefix)
        {
            StringBuilder builder = new StringBuilder();
            string        pfx1    = prefix + PrettyPrinter.Spacer;

            builder.AppendLine("[PyPackedRow " + RawData.Length + " bytes]");
            if (Descriptor.Columns != null)
            {
                foreach (var column in Descriptor.Columns)
                {
                    int   index = Descriptor.Columns.FindIndex(x => x.Name == column.Name);
                    PyRep value = values[index];
                    builder.AppendLine(pfx1 + "[\"" + column.Name + "\" => " + " [" + column.Type + "] " + value + "]");
                }
            }
            else
            {
                builder.AppendLine(pfx1 + "[Columns parsing failed!]");
            }
            return(builder.ToString());
        }
Example #17
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 #18
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 #19
0
        public PyAddress(PyRep args)
            : base(PyObjectType.Address)
        {
            if (args.Type != PyObjectType.ObjectData)
            {
                throw new InvalidDataException("PyAddress: Incorrect object type expected ObjectData got " + args.Type.ToString() + ".");
            }
            PyObject data = args as PyObject;

            if (data.Name != "carbon.common.script.net.machoNetAddress.MachoAddress")
            {
                throw new InvalidDataException("PyAddress: Unrecognized address name got " + data.Name + ".");
            }
            if (data.Arguments.Type != PyObjectType.Tuple)
            {
                throw new InvalidDataException("PyAddress: Address contents expected Tuple got " + data.Arguments.Type.ToString() + ".");
            }
            PyTuple tuple = data.Arguments as PyTuple;

            if (tuple.Items.Count < 3)
            {
                throw new InvalidDataException("PyAddress: Incorrect tupple size requires at least 3 got " + tuple.Items.Count + ".");
            }
            if (tuple.Items[0].Type != PyObjectType.Int)
            {
                throw new InvalidDataException("PyAddress: Address type expected Int got " + data.Arguments.Type.ToString() + ".");
            }
            addrType = (PyAddressType)tuple.Items[0].IntValue;
            if (addrType != PyAddressType.Any && tuple.Items.Count != 4)
            {
                throw new InvalidDataException("PyAddress: Incorrect tupple size requires 4 got " + tuple.Items.Count + ".");
            }
            switch (addrType)
            {
            case PyAddressType.Any:
                service = tuple.Items[1].StringValue;
                callID  = tuple.Items[2].IntValue;
                break;

            case PyAddressType.Node:
                addrID  = tuple.Items[1].IntValue;
                service = tuple.Items[2].StringValue;
                callID  = tuple.Items[3].IntValue;
                break;

            case PyAddressType.Client:
                addrID  = tuple.Items[1].IntValue;
                callID  = tuple.Items[2].IntValue;
                service = tuple.Items[3].StringValue;
                break;

            case PyAddressType.Broadcast:
                service       = tuple.Items[1].StringValue;
                broadcastType = tuple.Items[3].StringValue;
                break;

            default:
                throw new InvalidDataException("PyAddress: Unknown address type got " + addrType + ".");
            }
            RawOffset = args.RawOffset;
            RawSource = args.RawSource;
        }
Example #20
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     Checksum = source.ReadUInt32();
     Data     = context.ReadObject(source);
 }
Example #21
0
 public PyChecksumedStream(PyRep data)
     : base(PyObjectType.ChecksumedStream)
 {
     Data = data;
 }
Example #22
0
 public override void Decode(Unmarshal context, MarshalOpcode op)
 {
     Checksum = context.reader.ReadUInt32();
     Data     = context.ReadObject();
 }
Example #23
0
 public PyObject(string objectName, PyRep arguments)
     : base(PyObjectType.ObjectData)
 {
     Name      = objectName;
     Arguments = arguments;
 }
Example #24
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 #25
0
 public override void Decode(Unmarshal context, MarshalOpcode op)
 {
     Definition = context.ReadObject();
 }
Example #26
0
        public PyPacket(PyObject packetData)
            : base(PyObjectType.Packet)
        {
            if (!packetData.Name.StartsWith("carbon.common.script.net.machoNetPacket."))
            {
                // This is not a valid addressed packet.
                throw new InvalidDataException("PyPacket: Invalid typeString.");
            }
            typeString = packetData.Name;
            if (packetData.Arguments.Type != PyObjectType.Tuple)
            {
                // This is not a valid addressed packet.
                throw new InvalidDataException("PyPacket: Arguments not a Tuple.");
            }
            PyTuple args = packetData.Arguments as PyTuple;

            if (args.Items.Count != 9)
            {
                // This is not a correct size packet.
                throw new InvalidDataException("PyPacket: Expected Tuple size 9 got " + args.Items.Count + ".");
            }
            if (args.Items[0].Type != PyObjectType.Int)
            {
                // Incorrect packet type identifier.
                throw new InvalidDataException("PyPacket: Packet Type identifier.");
            }
            packetType = args.Items[0].IntValue;
            source     = new PyAddress(args.Items[1]);
            dest       = new PyAddress(args.Items[2]);
            userID     = 0;
            if (args.Items[3].Type == PyObjectType.Int)
            {
                userID = args.Items[3].IntValue;
            }
            else if (args.Items[3].Type != PyObjectType.None)
            {
                throw new InvalidDataException("PyPacket: UserID expected Int or None got " + args.Items[3].Type.ToString() + ".");
            }
            payload      = args.Items[4];
            namedPayload = args.Items[5];
            if (payload.Type != PyObjectType.Tuple && payload.Type != PyObjectType.Buffer)
            {
                throw new InvalidDataException("PyPacket: payload expected Tuple or Buffer got " + args.Items[3].Type.ToString() + ".");
            }
            if (namedPayload.Type != PyObjectType.None && namedPayload.Type != PyObjectType.Dict)
            {
                throw new InvalidDataException("PyPacket: named payload expected Dict or None got " + args.Items[3].Type.ToString() + ".");
            }
            RawOffset = args.RawOffset;
            RawSource = args.RawSource;
            try
            {
                if (typeString.EndsWith(".SessionChangeNotification"))
                {
                    payload = new SessionChangeNotification(payload as PyTuple);
                }
                else if (typeString.EndsWith(".CallRsp"))
                {
                    payload = new CallRsp(payload as PyTuple);
                }
                else if (typeString.EndsWith(".CallReq"))
                {
                    payload = new PyCallStream(payload as PyTuple);
                }
                else if (typeString.EndsWith(".ErrorResponse"))
                {
                    payload = new ErrorResponse(payload as PyTuple);
                }
                else if (typeString.EndsWith(".Notification"))
                {
                    payload = new NotificationStream(payload as PyTuple);
                }
            }
            catch (InvalidDataException e)
            {
                throw new InvalidDataException("PyPacket: Extended decode failed: " + e.Message, e);
            }
        }
Example #27
0
 public PySubStream(PyRep data)
     : base(PyObjectType.SubStream)
 {
     Data = data;
 }
Example #28
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     Definition = context.ReadObject(source);
 }
Example #29
0
        private PyRep analyseType1(PyObjectEx obj, out bool usedList, out bool usedDict)
        {
            usedDict = false;
            usedList = false;
            // Type1
            PyTuple headerTuple = obj.Header as PyTuple;

            if (headerTuple != null && headerTuple.Items.Count > 1)
            {
                int     headerCount = headerTuple.Items.Count;
                PyToken token       = headerTuple.Items[0] as PyToken;
                if (token != null)
                {
                    PyTuple tuple1      = null;
                    int     tuple1Count = 0;
                    if (headerCount > 1)
                    {
                        tuple1 = headerTuple.Items[1] as PyTuple;
                        if (tuple1 != null)
                        {
                            if (headerCount == 3 && token.Token == "eveexceptions.UserError")
                            {
                                return(new UserError(tuple1, headerTuple.Items[2] as PyDict));
                            }
                            tuple1Count = tuple1.Items.Count;
                            if (tuple1Count == 0)
                            {
                                if (headerCount == 3 && token.Token == "carbon.common.script.net.machoNetExceptions.WrongMachoNode")
                                {
                                    return(new WrongMachoNode(headerTuple.Items[2] as PyDict));
                                }
                            }
                            if (tuple1Count == 1)
                            {
                                if (token.Token == "blue.DBRowDescriptor")
                                {
                                    return(new DBRowDescriptor(headerTuple));
                                }
                                PyRep item1 = tuple1.Items[0];
                                if (item1 != null)
                                {
                                    if (token.Token == "__builtin__.set")
                                    {
                                        return(new BuiltinSet(item1 as PyList));
                                    }
                                    if (headerCount == 2 && token.Token == "collections.defaultdict")
                                    {
                                        PyToken tupleToken = item1 as PyToken;
                                        if (tupleToken.Token == "__builtin__.set")
                                        {
                                            usedDict = true;
                                            return(new DefaultDict(obj.Dictionary));
                                        }
                                    }
                                    if (token.Token == "carbon.common.script.net.objectCaching.CacheOK")
                                    {
                                        if (item1.StringValue == "CacheOK")
                                        {
                                            return(new CacheOK());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (token.Token == "carbon.common.script.net.GPSExceptions.GPSTransportClosed")
                    {
                        return(obj);
                    }
                    unknown.AppendLine("Unknown or malformed token: " + token.Token);
                }
            }
            return(obj);
        }
Example #30
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);
        }