Exemplo n.º 1
0
        private bool process(string filename, StreamWriter singleWriter)
        {
            // Does the file exist?
            if (!File.Exists(filename))
            {
                addText("File not found: " + filename);
                // No, fail!
                return(false);
            }
            byte[] data = null;
            using (var f = File.Open(filename, FileMode.Open))
            {
                if (f.Length == 0)
                {
                    if (singleWriter != null)
                    {
                        // Write the filename.
                        singleWriter.WriteLine(Path.GetFileName(filename));
                        // Write the decoded file.
                        singleWriter.WriteLine("Zero Length file.");
                    }
                    return(true);
                }
                data = new byte[f.Length];
                f.Read(data, 0, (int)f.Length);
                f.Close();
            }
            if (data == null)
            {
                // No data loaded.
                return(false);
            }
            // Is this a compressed file?
            if (data[0] == ZlibMarker)
            {
                // Yes, decompress it.
                data = Zlib.Decompress(data);
                if (data == null)
                {
                    // Decompress failed.
                    return(false);
                }
            }
            // Is this a proper python serial stream?
            if (data[0] != HeaderByte)
            {
                // No, is this a python file? If yes, ignore it but dont cause an error.
                if (data[0] == PythonMarker && decompilePython && !imbededPython)
                {
                    try
                    {
                        Bytecode code = new Bytecode();
                        code.load(data);
                        string outfile = null;
                        if (code.body != null)
                        {
                            Python.PyString fns = code.body.filename as Python.PyString;
                            if (fns != null)
                            {
                                outfile = fns.str;
                                outfile = outfile.Replace(':', '_');
                                outfile = outfile.Replace('\\', '-');
                                outfile = outfile.Replace('/', '-');
                            }
                        }
                        if (outfile != null)
                        {
                            string pyd = Path.GetDirectoryName(filename);
                            pyd += "\\py\\";
                            if (!Directory.Exists(pyd))
                            {
                                Directory.CreateDirectory(pyd);
                            }
                            outfile = pyd + "\\" + outfile + ".txt";
                            string dump = Python.PrettyPrinter.print(code, true);
                            File.WriteAllText(outfile, dump);
                        }
                    }
                    catch (Exception e)
                    {
                        string err = Path.GetFileName(filename) + Environment.NewLine + "Error: " + e.ToString();
                        // We, had an error but should still produce some kind of notice in the output.
                        addText(err + Environment.NewLine);
                        if (singleWriter != null)
                        {
                            //// Write the filename.
                            //singleWriter.WriteLine(Path.GetFileName(filename));
                            //// Write the decoded file.
                            //singleWriter.WriteLine("Python Decoder Error.");
                            //singleWriter.WriteLine(err);
                        }
                        else
                        {
                            //File.WriteAllText(filename + ".txt", err);
                        }
                        return(false);
                    }
                }
                return(data[0] == PythonMarker);
            }
            bool decodeDone = false;

            try
            {
                Unmarshal un = new Unmarshal();
                un.analizeInput = analizeInput;
                PyRep obj = un.Process(data);
                decodeDone = true;
                obj        = analyse(obj, filename);
                eveMarshal.PrettyPrinter printer = new eveMarshal.PrettyPrinter();
                printer.analizeInput    = analizeInput;
                printer.decompilePython = decompilePython;
                string decoded = printer.Print(obj);
                if (singleWriter != null)
                {
                    // Write the filename.
                    singleWriter.WriteLine(Path.GetFileName(filename));
                    // Write the decoded file.
                    singleWriter.Write(decoded);
                    singleWriter.Flush();
                }
                else
                {
                    File.WriteAllText(filename + ".txt", decoded);
                }
                if (un.unknown.Length > 0)
                {
                    addText(workingDirectory + Environment.NewLine);
                    addText(un.unknown.ToString() + Environment.NewLine);
                }
            }
            catch (Exception e)
            {
                string err = Path.GetFileName(filename) + Environment.NewLine + "Error: " + e.ToString();
                // We, had an error but should still produce some kind of notice in the output.
                addText(err + Environment.NewLine);
                if (singleWriter != null)
                {
                    // Write the filename.
                    singleWriter.WriteLine(Path.GetFileName(filename));
                    // Write the decoded file.
                    singleWriter.WriteLine(decodeDone ? "Printer Error. " : "Decoder Error.");
                    singleWriter.WriteLine(err);
                }
                else
                {
                    File.WriteAllText(filename + ".txt", err);
                }
                return(false);
            }
            return(true);
        }