Ejemplo n.º 1
0
 private void ComputeHashCore(string[] paths, DataGridViewRow[] rows, int iHashType)
 {
     for (int i = 0; i < paths.Length; i++)
     {
         try {
             byte[] buffer;
             DataGridViewProgressBarCell cell = (DataGridViewProgressBarCell)rows[i].Cells[2];
             using (FileHashStream stream = new FileHashStream(paths[i], ReportCallbackAsync, cell)) {
                 using (HashAlgorithm algorithm = CreateHashAlgorithm(iHashType)) {
                     buffer = algorithm.ComputeHash(stream);
                 }
                 if (stream.IsAborted)
                 {
                     cell.CalculatingStatus = HashCalcStatus.Aborted;
                     return;
                 }
             }
             StringBuilder builder = new StringBuilder(buffer.Length);
             for (int j = 0; j < buffer.Length; j++)
             {
                 builder.Append(buffer[j].ToString("X2"));
             }
             Invoke(new HashInvoker2(SetRowSync), new object[] { paths[i], builder.ToString(), rows[i] });
         }
         catch {
             Invoke(new HashInvoker2(SetRowSync), new object[] { paths[i], VALUE_ERROR, rows[i] });
         }
         if (fCancellationPending)
         {
             return;
         }
     }
 }
Ejemplo n.º 2
0
        protected async Task InternalCreateIRD(string savePath, CancellationToken cancellation, string isoPath = null)
        {
            byte[] buffer = new byte[Utilities.SectorSize];

            List <FileHash> fileList = new List <FileHash>();

            try
            {
                // First, get the partition table of this iso
                using (Stream fs = Open())
                {
                    if (fs == null)
                    {
                        return;
                    }
                    PS3CDReader reader = new PS3CDReader(fs);
                    List <DirectoryMemberInformation> files = reader.Members.Where(d => d.IsFile).Distinct().ToList();
                    StartOfDataSector = files.First().StartSector;
                    EndOfDataSector   = files.Last().StartSector + files.Last().TotalSectors;

                    var updateFile = files.FirstOrDefault(
                        d => d.Path.Equals("/PS3_UPDATE/PS3UPDAT.PUP", StringComparison.OrdinalIgnoreCase));
                    long updateOffset = updateFile != null ? updateFile.StartSector * Utilities.SectorSize : 0;

                    fileList.AddRange(files.Select(d => new FileHash(d.StartSector, d.Length)));

                    if (cancellation.IsCancellationRequested)
                    {
                        return;
                    }

                    IrdFile irdFile = IrdFile.New();

                    using (Stream s = reader.OpenFile("PS3_GAME\\PARAM.SFO", FileMode.Open, FileAccess.Read))
                    {
                        ParamSfo sfo = ParamSfo.Load(s);
                        irdFile.AppVersion  = sfo.GetStringValue("APP_VER");
                        irdFile.GameVersion = sfo.GetStringValue("VERSION");
                        irdFile.GameID      = sfo.GetStringValue("TITLE_ID");
                        irdFile.GameName    = sfo.GetStringValue("TITLE");
                    }

                    Interaction.Instance.ReportMessage("Processing " + (string.IsNullOrEmpty(irdFile.GameName)
                                                                            ? irdFile.GameID
                                                                            : irdFile.GameName));

                    irdFile.UpdateVersion = Utilities.FindUpdateVersion(fs, updateOffset);

                    using (FileStream isoStream = string.IsNullOrEmpty(isoPath)
                                                      ? null
                                                      : new FileStream(isoPath, FileMode.Create, FileAccess.Write))
                    {
                        if (isoStream != null)
                        {
                            isoStream.SetLength(TotalSectors * Utilities.SectorSize);
                        }

                        using (IOStream ioStream = new IOStream(fs, isoStream))
                        {
                            ioStream.Seek(0, SeekOrigin.Begin);

                            // Read the header, until StartOfDataSector (in sectors)
                            irdFile.Header = new MemoryStream();
                            for (int i = 0; i < StartOfDataSector; i++)
                            {
                                int bytesRead = ioStream.Read(buffer, 0, buffer.Length);
                                irdFile.Header.Write(buffer, 0, bytesRead);
                                Interaction.Instance.ReportProgress(i);
                                if (cancellation.IsCancellationRequested)
                                {
                                    return;
                                }
                            }
                            irdFile.ExtractAuthData();

                            // Fix the regions for the actual interesting data
                            Regions.First().Start   = (uint)StartOfDataSector;
                            Regions.First().Length -= (uint)StartOfDataSector;
                            Regions.Last().Length   = (uint)EndOfDataSector - Regions.Last().Start;

                            // Now, we should calculate the md5 sums of all regions
                            Interaction.Instance.ReportMessage("Calculating hashes for " + Regions.Length + " regions.");
                            for (int i = 0; i < Regions.Length; i++)
                            {
                                // Calculate the hash
                                Interaction.Instance.ReportMessage(
                                    "Calculate hash for region " + i + " (" + Regions[i].Start.ToString("X2") + "-" +
                                    Regions[i].End.ToString("X2") + ")");

                                ioStream.Seek(Regions[i].Start * Utilities.SectorSize, SeekOrigin.Begin);

                                using (FileHashStream fhs = new FileHashStream(fileList, Regions[i].Start))
                                {
                                    await Regions[i].CopyRegion(cancellation, ioStream, fhs);
                                    if (cancellation.IsCancellationRequested)
                                    {
                                        return;
                                    }
                                    irdFile.RegionHashes.Add(Regions[i].SourceHash);
                                    Interaction.Instance.ReportMessage("Region " + i + " hash: " +
                                                                       Regions[i].SourceHash.AsString());
                                }
                            }

                            ioStream.Seek(EndOfDataSector * Utilities.SectorSize, SeekOrigin.Begin);
                            irdFile.Footer = new MemoryStream();
                            for (long i = EndOfDataSector; i < TotalSectors; i++)
                            {
                                int bytesRead = ioStream.Read(buffer, 0, buffer.Length);
                                irdFile.Footer.Write(buffer, 0, bytesRead);
                                Interaction.Instance.ReportProgress((int)(ioStream.Position / Utilities.SectorSize));
                                if (cancellation.IsCancellationRequested)
                                {
                                    return;
                                }
                            }
                        }
                    }
                    irdFile.FileHashes = fileList.ToDictionary(t => t.StartSector, t => t.Hash);
                    irdFile.Save(savePath);
                    Interaction.Instance.ReportMessage("All done, IRD file saved to " + savePath);
                }
            }
            catch (BadReadException e)
            {
                Interaction.Instance.ReportMessage(e.Message, ReportType.Fail);
            }
            catch (AuthenticationException e)
            {
                Interaction.Instance.ReportMessage(e.Message, ReportType.Fail);
            }
        }
Ejemplo n.º 3
0
 private void ComputeHashCore(string[] paths, DataGridViewRow[] rows, int iHashType)
 {
     for(int i = 0; i < paths.Length; i++) {
         try {
             byte[] buffer;
             DataGridViewProgressBarCell cell = (DataGridViewProgressBarCell)rows[i].Cells[2];
             using(FileHashStream stream = new FileHashStream(paths[i], ReportCallbackAsync, cell)) {
                 using(HashAlgorithm algorithm = CreateHashAlgorithm(iHashType)) {
                     buffer = algorithm.ComputeHash(stream);
                 }
                 if(stream.IsAborted) {
                     cell.CalculatingStatus = HashCalcStatus.Aborted;
                     return;
                 }
             }
             StringBuilder builder = new StringBuilder(buffer.Length);
             for(int j = 0; j < buffer.Length; j++) {
                 builder.Append(buffer[j].ToString("X2"));
             }
             Invoke(new HashInvoker2(SetRowSync), new object[] { paths[i], builder.ToString(), rows[i] });
         }
         catch {
             Invoke(new HashInvoker2(SetRowSync), new object[] { paths[i], VALUE_ERROR, rows[i] });
         }
         if(fCancellationPending) {
             return;
         }
     }
 }