Ejemplo n.º 1
0
        private string ExtractFiles(string decryptedPKGFileName, string encryptedPKGFileName)
        {
            Exception exception;

            try
            {
                int    num         = 0x1400000;
                uint   num2        = 0;
                uint   num3        = 0;
                uint   num4        = 0;
                long   sourceIndex = 0L;
                string str         = null;
                if (encryptedPKGFileName.Contains(@"\"))
                {
                    str = encryptedPKGFileName.Substring(encryptedPKGFileName.LastIndexOf(@"\")).Replace(@"\", "").Replace(".pkg", "");
                }
                else
                {
                    str = encryptedPKGFileName.Replace(".pkg", "");
                }
                string path = "temp/" + str;
                Directory.CreateDirectory("temp");
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, true);
                    Thread.Sleep(100);
                    Directory.CreateDirectory(path);
                    Thread.Sleep(100);
                }
                byte[]       sourceArray      = new byte[0x4e200];
                byte[]       buffer3          = new byte[8];
                byte[]       destinationArray = new byte[4];
                byte[]       buffer5          = new byte[4];
                byte[]       buffer6          = new byte[4];
                byte[]       buffer7          = new byte[4];
                byte[]       buffer8          = new byte[4];
                byte[]       buffer9          = new byte[4];
                byte[]       buffer10         = new byte[4];
                byte[]       buffer11         = new byte[4];
                byte[]       buffer12         = new byte[0x20];
                byte[]       buffer13         = new byte[8];
                byte         num6             = 0;
                byte         num7             = 0;
                bool         flag             = false;
                Stream       input            = new FileStream(decryptedPKGFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader reader           = new BinaryReader(input);
                Stream       stream2          = new FileStream(encryptedPKGFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader brEncrPKG        = new BinaryReader(stream2);
                input.Seek(0L, SeekOrigin.Begin);
                sourceArray = reader.ReadBytes(sourceArray.Length);
                sourceIndex = 0L;
                num4        = 0;
                Array.Copy(sourceArray, 0, buffer5, 0, buffer5.Length);
                Array.Reverse(buffer5);
                uint num9 = BitConverter.ToUInt32(buffer5, 0) / 0x20;
                Array.Copy(sourceArray, 12, destinationArray, 0, destinationArray.Length);
                Array.Reverse(destinationArray);
                uint num10 = BitConverter.ToUInt32(destinationArray, 0);
                input.Seek(0L, SeekOrigin.Begin);
                sourceArray = reader.ReadBytes((int)num10);
                if (num9 < 0)
                {
                    return("");
                }
                for (int i = 0; i < num9; i++)
                {
                    byte[]       buffer2;
                    BinaryWriter writer;
                    double       num14;
                    ulong        num15;
                    ulong        num16;
                    ulong        num17;
                    Array.Copy(sourceArray, sourceIndex + 12L, buffer8, 0L, (long)buffer8.Length);
                    Array.Reverse(buffer8);
                    num2 = BitConverter.ToUInt32(buffer8, 0) + num4;
                    Array.Copy(sourceArray, sourceIndex + 20L, buffer9, 0L, (long)buffer9.Length);
                    Array.Reverse(buffer9);
                    num3 = BitConverter.ToUInt32(buffer9, 0);
                    Array.Copy(sourceArray, sourceIndex, buffer10, 0L, (long)buffer10.Length);
                    Array.Reverse(buffer10);
                    uint num12 = BitConverter.ToUInt32(buffer10, 0);
                    Array.Copy(sourceArray, sourceIndex + 4L, buffer11, 0L, (long)buffer11.Length);
                    Array.Reverse(buffer11);
                    uint num13 = BitConverter.ToUInt32(buffer11, 0);
                    num6     = sourceArray[(int)((IntPtr)(sourceIndex + 0x18L))];
                    num7     = sourceArray[(int)((IntPtr)(sourceIndex + 0x1bL))];
                    buffer12 = new byte[num13];
                    Array.Copy(sourceArray, (long)num12, buffer12, 0L, (long)num13);
                    string str5 = ByteArrayToAscii(buffer12, 0, buffer12.Length, true);
                    string str6 = string.Concat((IEnumerable <string>)(from b in buffer12 select b.ToString("x2")));
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                        Thread.Sleep(100);
                    }
                    FileStream output = null;
                    if ((num7 == 4) && (num3 == 0))
                    {
                        flag = false;
                    }
                    else
                    {
                        flag = true;
                    }
                    if (num6 == 0x90)
                    {
                        string        str7   = (path + @"\" + str5).Replace("/", @"\");
                        DirectoryInfo parent = Directory.GetParent(str7);
                        if (!Directory.Exists(parent.ToString()))
                        {
                            Directory.CreateDirectory(parent.ToString());
                        }
                        output = new FileStream(str7, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                    }
                    else
                    {
                        Array.Copy(this.DecryptData((int)num13, (long)num12, (long)this.uiEncryptedFileStartOffset, this.PS3AesKey, stream2, brEncrPKG), 0L, buffer12, 0L, (long)num13);
                        str5 = ByteArrayToAscii(buffer12, 0, buffer12.Length, true);
                        if (!flag)
                        {
                            try
                            {
                            }
                            catch (Exception exception1)
                            {
                                exception = exception1;
                                str5      = i.ToString() + ".raw";
                            }
                        }
                        else if (str5 == "PARAM.SFO")
                        {
                            try
                            {
                                output = new FileStream(path + @"\" + str5, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                            }
                            catch (Exception exception2)
                            {
                                exception = exception2;
                                str5      = i.ToString() + ".raw";
                            }
                        }
                    }
                    if (((num6 == 0x90) && flag) && (str5 == "PARAM.SFO"))
                    {
                        writer = new BinaryWriter(output);
                        input.Seek((long)num2, SeekOrigin.Begin);
                        num14 = ((double)num3) / ((double)num);
                        num15 = (ulong)Math.Floor(num14);
                        num16 = ((ulong)num3) % ((ulong)num);
                        if (num16 > 0L)
                        {
                            num15 += (ulong)1L;
                        }
                        buffer2 = new byte[num];
                        num17   = 0L;
                        while (num17 < num15)
                        {
                            if ((num16 > 0L) && (num17 == (num15 - ((ulong)1L))))
                            {
                                buffer2 = new byte[num16];
                            }
                            reader.Read(buffer2, 0, buffer2.Length);
                            writer.Write(buffer2);
                            num17 += (ulong)1L;
                        }
                        output.Close();
                        writer.Close();
                    }
                    if (((num6 != 0x90) && flag) && (str5 == "PARAM.SFO"))
                    {
                        writer = new BinaryWriter(output);
                        input.Seek((long)num2, SeekOrigin.Begin);
                        num14 = ((double)num3) / ((double)num);
                        num15 = (ulong)Math.Floor(num14);
                        num16 = ((ulong)num3) % ((ulong)num);
                        if (num16 > 0L)
                        {
                            num15 += (ulong)1L;
                        }
                        buffer2 = new byte[num];
                        long length = 0L;
                        for (num17 = 0L; num17 < num15; num17 += (ulong)1L)
                        {
                            if ((num16 > 0L) && (num17 == (num15 - ((ulong)1L))))
                            {
                                buffer2 = new byte[num16];
                            }
                            byte[] buffer = this.DecryptData(buffer2.Length, num2 + length, (long)this.uiEncryptedFileStartOffset, this.PS3AesKey, stream2, brEncrPKG);
                            length = buffer2.Length;
                            writer.Write(buffer, 0, buffer2.Length);
                        }
                        output.Close();
                        writer.Close();
                    }
                    sourceIndex += 0x20L;
                }
                stream2.Close();
                brEncrPKG.Close();
                input.Close();
                reader.Close();
                if (File.Exists(decryptedPKGFileName))
                {
                    File.Delete(decryptedPKGFileName);
                }
                Console.WriteLine("Creating EDAT");
                string  outFile = null;
                C00EDAT cedat   = new C00EDAT();
                return(cedat.makeedat(path + "/PARAM.SFO", outFile));
            }
            catch (Exception exception3)
            {
                exception = exception3;
                SystemSounds.Beep.Play();
                return("");
            }
        }
        private string ExtractFiles(string decryptedPKGFileName, string encryptedPKGFileName)
        {
            try
            {
                int    twentyMb            = 1024 * 1024 * 20;
                UInt32 ExtractedFileOffset = 0;
                UInt32 ExtractedFileSize   = 0;
                UInt32 OffsetShift         = 0;
                long   positionIdx         = 0;
                string pkgname             = null;
                if (encryptedPKGFileName.Contains("\\"))
                {
                    string pkgname1 = encryptedPKGFileName.Substring(encryptedPKGFileName.LastIndexOf("\\"));
                    string pkgname2 = pkgname1.Replace("\\", "");
                    pkgname = pkgname2.Replace(".pkg", "");
                }
                else
                {
                    pkgname = encryptedPKGFileName.Replace(".pkg", "");
                }


                String WorkDir = "temp/" + pkgname;

                // WorkDir = decryptedPKGFileName + ".EXT";
                Directory.CreateDirectory("temp");
                if (Directory.Exists(WorkDir))
                {
                    Directory.Delete(WorkDir, true);
                    System.Threading.Thread.Sleep(100);

                    Directory.CreateDirectory(WorkDir);
                    System.Threading.Thread.Sleep(100);
                }

                byte[] FileTable = new byte[320000];
                byte[] dumpFile;
                byte[] sdkVer          = new byte[8];
                byte[] firstFileOffset = new byte[4];
                byte[] firstNameOffset = new byte[4];
                byte[] fileNr          = new byte[4];
                byte[] isDir           = new byte[4];
                byte[] Offset          = new byte[4];
                byte[] Size            = new byte[4];
                byte[] NameOffset      = new byte[4];
                byte[] NameSize        = new byte[4];
                byte[] Name            = new byte[32];
                byte[] bootMagic       = new byte[8];
                byte   contentType     = 0;
                byte   fileType        = 0;
                bool   isFile          = false;

                Stream       decrPKGReadStream = new FileStream(decryptedPKGFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader brDecrPKG         = new BinaryReader(decrPKGReadStream);

                Stream       encrPKGReadStream = new FileStream(encryptedPKGFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader brEncrPKG         = new BinaryReader(encrPKGReadStream);

                //Read the file Table
                decrPKGReadStream.Seek((long)0, SeekOrigin.Begin);
                FileTable = brDecrPKG.ReadBytes(FileTable.Length);

                positionIdx = 0;

                OffsetShift = 0;   //Shift Relative to os.raw

                Array.Copy(FileTable, 0, firstNameOffset, 0, firstNameOffset.Length);
                Array.Reverse(firstNameOffset);
                uint uifirstNameOffset = BitConverter.ToUInt32(firstNameOffset, 0);

                uint uiFileNr = uifirstNameOffset / 32;

                Array.Copy(FileTable, 12, firstFileOffset, 0, firstFileOffset.Length);
                Array.Reverse(firstFileOffset);
                uint uifirstFileOffset = BitConverter.ToUInt32(firstFileOffset, 0);

                //Read the file Table
                decrPKGReadStream.Seek((long)0, SeekOrigin.Begin);
                FileTable = brDecrPKG.ReadBytes((int)uifirstFileOffset);

                //If number of files is negative then something is wrong...
                if ((int)uiFileNr < 0)
                {
                    //  txtResults.Text += "ERROR: An error occured during the files extraction process cause of a decryption error";
                    //  SystemSounds.Beep.Play();
                    return("");
                }



                //Table:
                //0-3         4-7         8-11        12-15       16-19       20-23       24-27       28-31
                //|name loc | |name size| |   NULL  | |file loc | |  NULL   | |file size| |cont type| |   NULL  |

                for (int ii = 0; ii < (int)uiFileNr; ii++)
                {
                    Array.Copy(FileTable, positionIdx + 12, Offset, 0, Offset.Length);
                    Array.Reverse(Offset);
                    ExtractedFileOffset = BitConverter.ToUInt32(Offset, 0) + OffsetShift;

                    Array.Copy(FileTable, positionIdx + 20, Size, 0, Size.Length);
                    Array.Reverse(Size);
                    ExtractedFileSize = BitConverter.ToUInt32(Size, 0);

                    Array.Copy(FileTable, positionIdx, NameOffset, 0, NameOffset.Length);
                    Array.Reverse(NameOffset);
                    uint ExtractedFileNameOffset = BitConverter.ToUInt32(NameOffset, 0);

                    Array.Copy(FileTable, positionIdx + 4, NameSize, 0, NameSize.Length);
                    Array.Reverse(NameSize);
                    uint ExtractedFileNameSize = BitConverter.ToUInt32(NameSize, 0);

                    contentType = FileTable[positionIdx + 24];
                    fileType    = FileTable[positionIdx + 27];

                    Name = new byte[ExtractedFileNameSize];
                    Array.Copy(FileTable, (int)ExtractedFileNameOffset, Name, 0, ExtractedFileNameSize);
                    string ExtractedFileName = ByteArrayToAscii(Name, 0, Name.Length, true);
                    // txtResults.Text += "ExtractedFileName = " + ExtractedFileName;
                    // txtResults.Text += "ExtractedFileNameOffset = " + ExtractedFileNameOffset;
                    //  txtResults.Text += "ExtractedFileNameSize = " + ExtractedFileNameSize;
                    string dataString = String.Concat(Name.Select(b => b.ToString("x2")));
                    //  txtResults.Text += "name = " + dataString;



                    //Write Directory
                    if (!Directory.Exists(WorkDir))
                    {
                        Directory.CreateDirectory(WorkDir);
                        System.Threading.Thread.Sleep(100);
                    }

                    FileStream ExtractedFileWriteStream = null;

                    //File / Directory
                    if ((fileType == 0x04) && (ExtractedFileSize == 0x00))
                    {
                        isFile = false;
                    }
                    else
                    {
                        isFile = true;
                    }

                    //contentType == 0x90 = PSP file/dir
                    if (contentType == 0x90)
                    {
                        string FileDir = WorkDir + "\\" + ExtractedFileName;
                        FileDir = FileDir.Replace("/", "\\");
                        DirectoryInfo FileDirectory = Directory.GetParent(FileDir);

                        if (!Directory.Exists(FileDirectory.ToString()))
                        {
                            Directory.CreateDirectory(FileDirectory.ToString());
                        }
                        ExtractedFileWriteStream = new FileStream(FileDir, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                    }
                    else
                    {
                        //contentType == (0x80 || 0x00) = PS3 file/dir
                        //fileType == 0x01 = NPDRM File
                        //fileType == 0x03 = Raw File
                        //fileType == 0x04 = Directory

                        //Decrypt PS3 Filename
                        byte[] DecryptedData = DecryptData((int)ExtractedFileNameSize, (long)ExtractedFileNameOffset, (long)uiEncryptedFileStartOffset, PS3AesKey, encrPKGReadStream, brEncrPKG);
                        Array.Copy(DecryptedData, 0, Name, 0, ExtractedFileNameSize);
                        ExtractedFileName = ByteArrayToAscii(Name, 0, Name.Length, true);

                        StreamReader reader = null;
                        // string dataString = String.Concat(FileTable.Select(b => b.ToString("x2")));
                        //  sw3.AutoFlush = true;
                        //  Console.SetOut(sw3);
                        //  Console.WriteLine(ExtractedFileName);
                        //   Console.ReadKey();
                        //  reader = new StreamReader(ExtractedFileName);
                        if (!isFile)
                        {
                            //Directory
                            try
                            {
                                //  if (!Directory.Exists(ExtractedFileName))
                                //   Directory.CreateDirectory(WorkDir + "\\" + ExtractedFileName);
                            }
                            catch (Exception ex)
                            {
                                //This should not happen xD
                                ExtractedFileName = ii.ToString() + ".raw";
                                // if (!Directory.Exists(ExtractedFileName))
                                //  Directory.CreateDirectory(WorkDir + "\\" + ExtractedFileName);
                            }
                        }
                        else if (ExtractedFileName == "PARAM.SFO")
                        {
                            //File
                            try
                            {
                                ExtractedFileWriteStream = new FileStream(WorkDir + "\\" + ExtractedFileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                            }
                            catch (Exception ex)
                            {
                                //This should not happen xD
                                ExtractedFileName = ii.ToString() + ".raw";
                                // ExtractedFileWriteStream = new FileStream(WorkDir + "\\" + ExtractedFileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                            }
                        }
                    }

                    if (contentType == 0x90 && isFile && ExtractedFileName == "PARAM.SFO")
                    {
                        //Read/Write File
                        BinaryWriter ExtractedFile = new BinaryWriter(ExtractedFileWriteStream);
                        decrPKGReadStream.Seek((long)ExtractedFileOffset, SeekOrigin.Begin);

                        // Pieces calculation
                        double division = (double)ExtractedFileSize / (double)twentyMb;
                        UInt64 pieces   = (UInt64)Math.Floor(division);
                        UInt64 mod      = (UInt64)ExtractedFileSize % (UInt64)twentyMb;
                        if (mod > 0)
                        {
                            pieces += 1;
                        }

                        dumpFile = new byte[twentyMb];
                        for (UInt64 i = 0; i < pieces; i++)
                        {
                            //If we have a mod and this is the last piece then...
                            if ((mod > 0) && (i == (pieces - 1)))
                            {
                                dumpFile = new byte[mod];
                            }

                            //Fill buffer
                            brDecrPKG.Read(dumpFile, 0, dumpFile.Length);

                            ExtractedFile.Write(dumpFile);

                            //   Application.DoEvents();
                        }

                        ExtractedFileWriteStream.Close();
                        ExtractedFile.Close();
                    }


                    if (contentType != 0x90 && isFile && ExtractedFileName == "PARAM.SFO")
                    //  if (contentType != 0x90 && isFile && Name == Name2)
                    {
                        //Read/Write File
                        BinaryWriter ExtractedFile = new BinaryWriter(ExtractedFileWriteStream);
                        decrPKGReadStream.Seek((long)ExtractedFileOffset, SeekOrigin.Begin);

                        // Pieces calculation
                        double division = (double)ExtractedFileSize / (double)twentyMb;
                        UInt64 pieces   = (UInt64)Math.Floor(division);
                        UInt64 mod      = (UInt64)ExtractedFileSize % (UInt64)twentyMb;
                        if (mod > 0)
                        {
                            pieces += 1;
                        }

                        dumpFile = new byte[twentyMb];
                        long elapsed = 0;
                        for (UInt64 i = 0; i < pieces; i++)
                        {
                            //If we have a mod and this is the last piece then...
                            if ((mod > 0) && (i == (pieces - 1)))
                            {
                                dumpFile = new byte[mod];
                            }

                            //Fill buffer
                            byte[] DecryptedData = DecryptData(dumpFile.Length, (long)ExtractedFileOffset + elapsed, (long)uiEncryptedFileStartOffset, PS3AesKey, encrPKGReadStream, brEncrPKG);
                            elapsed = +dumpFile.Length;

                            //To avoid decryption pad we use dumpFile.Length that's the actual decrypted file size!
                            ExtractedFile.Write(DecryptedData, 0, dumpFile.Length);

                            //  Application.DoEvents();
                        }

                        ExtractedFileWriteStream.Close();
                        ExtractedFile.Close();
                    }

                    positionIdx = positionIdx + 32;

                    // progressBar.PerformStep();
                    // Application.DoEvents();
                }

                //     Application.DoEvents();

                //Close File
                encrPKGReadStream.Close();
                brEncrPKG.Close();

                decrPKGReadStream.Close();
                brDecrPKG.Close();

                //Delete decrypted file
                if (File.Exists(decryptedPKGFileName))
                {
                    File.Delete(decryptedPKGFileName);
                }

                //  txtResults.Text += "SUCCESS: Pkg extracted and decrypted successfully.";
                // SystemSounds.Beep.Play();

                //  fileDirBrowser.PathFileDir = "Open Encrypted PKG file here first...";

                // progressBar.Value = 0;
                Console.WriteLine("Creating EDAT");
                string  outFile  = null;
                C00EDAT instance = new C00EDAT();
                outFile = instance.makeedat(WorkDir + "/PARAM.SFO", outFile);
                //Console.WriteLine(outFile);
                return(outFile);
            }

            catch (Exception ex)
            {
                // txtResults.Text += "ERROR: An error occured during the files extraction process ";
                SystemSounds.Beep.Play();
                return("");
            }
        }
Ejemplo n.º 3
0
 private string ExtractFiles(string decryptedPKGFileName, string encryptedPKGFileName)
 {
     Exception exception;
     try
     {
         int num = 0x1400000;
         uint num2 = 0;
         uint num3 = 0;
         uint num4 = 0;
         long sourceIndex = 0L;
         string str = null;
         if (encryptedPKGFileName.Contains(@"\"))
         {
             str = encryptedPKGFileName.Substring(encryptedPKGFileName.LastIndexOf(@"\")).Replace(@"\", "").Replace(".pkg", "");
         }
         else
         {
             str = encryptedPKGFileName.Replace(".pkg", "");
         }
         string path = "temp/" + str;
         Directory.CreateDirectory("temp");
         if (Directory.Exists(path))
         {
             Directory.Delete(path, true);
             Thread.Sleep(100);
             Directory.CreateDirectory(path);
             Thread.Sleep(100);
         }
         byte[] sourceArray = new byte[0x4e200];
         byte[] buffer3 = new byte[8];
         byte[] destinationArray = new byte[4];
         byte[] buffer5 = new byte[4];
         byte[] buffer6 = new byte[4];
         byte[] buffer7 = new byte[4];
         byte[] buffer8 = new byte[4];
         byte[] buffer9 = new byte[4];
         byte[] buffer10 = new byte[4];
         byte[] buffer11 = new byte[4];
         byte[] buffer12 = new byte[0x20];
         byte[] buffer13 = new byte[8];
         byte num6 = 0;
         byte num7 = 0;
         bool flag = false;
         Stream input = new FileStream(decryptedPKGFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
         BinaryReader reader = new BinaryReader(input);
         Stream stream2 = new FileStream(encryptedPKGFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
         BinaryReader brEncrPKG = new BinaryReader(stream2);
         input.Seek(0L, SeekOrigin.Begin);
         sourceArray = reader.ReadBytes(sourceArray.Length);
         sourceIndex = 0L;
         num4 = 0;
         Array.Copy(sourceArray, 0, buffer5, 0, buffer5.Length);
         Array.Reverse(buffer5);
         uint num9 = BitConverter.ToUInt32(buffer5, 0) / 0x20;
         Array.Copy(sourceArray, 12, destinationArray, 0, destinationArray.Length);
         Array.Reverse(destinationArray);
         uint num10 = BitConverter.ToUInt32(destinationArray, 0);
         input.Seek(0L, SeekOrigin.Begin);
         sourceArray = reader.ReadBytes((int) num10);
         if (num9 < 0)
         {
             return "";
         }
         for (int i = 0; i < num9; i++)
         {
             byte[] buffer2;
             BinaryWriter writer;
             double num14;
             ulong num15;
             ulong num16;
             ulong num17;
             Array.Copy(sourceArray, sourceIndex + 12L, buffer8, 0L, (long) buffer8.Length);
             Array.Reverse(buffer8);
             num2 = BitConverter.ToUInt32(buffer8, 0) + num4;
             Array.Copy(sourceArray, sourceIndex + 20L, buffer9, 0L, (long) buffer9.Length);
             Array.Reverse(buffer9);
             num3 = BitConverter.ToUInt32(buffer9, 0);
             Array.Copy(sourceArray, sourceIndex, buffer10, 0L, (long) buffer10.Length);
             Array.Reverse(buffer10);
             uint num12 = BitConverter.ToUInt32(buffer10, 0);
             Array.Copy(sourceArray, sourceIndex + 4L, buffer11, 0L, (long) buffer11.Length);
             Array.Reverse(buffer11);
             uint num13 = BitConverter.ToUInt32(buffer11, 0);
             num6 = sourceArray[(int) ((IntPtr) (sourceIndex + 0x18L))];
             num7 = sourceArray[(int) ((IntPtr) (sourceIndex + 0x1bL))];
             buffer12 = new byte[num13];
             Array.Copy(sourceArray, (long) num12, buffer12, 0L, (long) num13);
             string str5 = ByteArrayToAscii(buffer12, 0, buffer12.Length, true);
             string str6 = string.Concat((IEnumerable<string>) (from b in buffer12 select b.ToString("x2")));
             if (!Directory.Exists(path))
             {
                 Directory.CreateDirectory(path);
                 Thread.Sleep(100);
             }
             FileStream output = null;
             if ((num7 == 4) && (num3 == 0))
             {
                 flag = false;
             }
             else
             {
                 flag = true;
             }
             if (num6 == 0x90)
             {
                 string str7 = (path + @"\" + str5).Replace("/", @"\");
                 DirectoryInfo parent = Directory.GetParent(str7);
                 if (!Directory.Exists(parent.ToString()))
                 {
                     Directory.CreateDirectory(parent.ToString());
                 }
                 output = new FileStream(str7, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
             }
             else
             {
                 Array.Copy(this.DecryptData((int) num13, (long) num12, (long) this.uiEncryptedFileStartOffset, this.PS3AesKey, stream2, brEncrPKG), 0L, buffer12, 0L, (long) num13);
                 str5 = ByteArrayToAscii(buffer12, 0, buffer12.Length, true);
                 if (!flag)
                 {
                     try
                     {
                     }
                     catch (Exception exception1)
                     {
                         exception = exception1;
                         str5 = i.ToString() + ".raw";
                     }
                 }
                 else if (str5 == "PARAM.SFO")
                 {
                     try
                     {
                         output = new FileStream(path + @"\" + str5, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                     }
                     catch (Exception exception2)
                     {
                         exception = exception2;
                         str5 = i.ToString() + ".raw";
                     }
                 }
             }
             if (((num6 == 0x90) && flag) && (str5 == "PARAM.SFO"))
             {
                 writer = new BinaryWriter(output);
                 input.Seek((long) num2, SeekOrigin.Begin);
                 num14 = ((double) num3) / ((double) num);
                 num15 = (ulong) Math.Floor(num14);
                 num16 = ((ulong) num3) % ((ulong) num);
                 if (num16 > 0L)
                 {
                     num15 += (ulong) 1L;
                 }
                 buffer2 = new byte[num];
                 num17 = 0L;
                 while (num17 < num15)
                 {
                     if ((num16 > 0L) && (num17 == (num15 - ((ulong) 1L))))
                     {
                         buffer2 = new byte[num16];
                     }
                     reader.Read(buffer2, 0, buffer2.Length);
                     writer.Write(buffer2);
                     num17 += (ulong) 1L;
                 }
                 output.Close();
                 writer.Close();
             }
             if (((num6 != 0x90) && flag) && (str5 == "PARAM.SFO"))
             {
                 writer = new BinaryWriter(output);
                 input.Seek((long) num2, SeekOrigin.Begin);
                 num14 = ((double) num3) / ((double) num);
                 num15 = (ulong) Math.Floor(num14);
                 num16 = ((ulong) num3) % ((ulong) num);
                 if (num16 > 0L)
                 {
                     num15 += (ulong) 1L;
                 }
                 buffer2 = new byte[num];
                 long length = 0L;
                 for (num17 = 0L; num17 < num15; num17 += (ulong) 1L)
                 {
                     if ((num16 > 0L) && (num17 == (num15 - ((ulong) 1L))))
                     {
                         buffer2 = new byte[num16];
                     }
                     byte[] buffer = this.DecryptData(buffer2.Length, num2 + length, (long) this.uiEncryptedFileStartOffset, this.PS3AesKey, stream2, brEncrPKG);
                     length = buffer2.Length;
                     writer.Write(buffer, 0, buffer2.Length);
                 }
                 output.Close();
                 writer.Close();
             }
             sourceIndex += 0x20L;
         }
         stream2.Close();
         brEncrPKG.Close();
         input.Close();
         reader.Close();
         if (File.Exists(decryptedPKGFileName))
         {
             File.Delete(decryptedPKGFileName);
         }
         Console.WriteLine("Creating EDAT");
         string outFile = null;
         C00EDAT cedat = new C00EDAT();
         return cedat.makeedat(path + "/PARAM.SFO", outFile);
     }
     catch (Exception exception3)
     {
         exception = exception3;
         SystemSounds.Beep.Play();
         return "";
     }
 }