Пример #1
0
        private void FixSectorOffsets(FileStream iso, string godPath)
        {
            int sector, size, offset;

            byte[]           buffer;
            Queue <DirEntry> directories = new Queue <DirEntry>();

            buffer = File.ReadAllBytes(godPath);
            // offset type?
            if ((buffer[0x391] & 0x40) != 0x40)
            {
                return;
            }
            // calculate the offset
            offset = BitConverter.ToInt32(buffer, 0x395);
            if (offset == 0)
            {
                return;
            }
            offset *= 2;
            offset -= 34;

            buffer       = new byte[4];
            iso.Position = 0x10014;
            iso.Read(buffer, 0, 4);
            sector = BitConverter.ToInt32(buffer, 0);
            if (sector > 0)
            {
                sector -= offset;
                byte[] corrected = BitConverter.GetBytes(sector);
                iso.Position -= 4;
                iso.Write(corrected, 0, 4);
                iso.Read(buffer, 0, 4);
                size = BitConverter.ToInt32(buffer, 0);
                directories.Enqueue(new DirEntry(sector, size));
            }

            while (directories.Count > 0)
            {
                DirEntry dirEntry = directories.Dequeue();
                iso.Position = dirEntry.StartPos();

                while ((iso.Position + 4) < dirEntry.EndPos())
                {
                    // crossed a sector boundary?
                    if ((iso.Position + 4) / 2048L > iso.Position / 2048L)
                    {
                        iso.Position += 2048L - (iso.Position % 2048L);
                    }
                    // read subtrees
                    iso.Read(buffer, 0, 4);
                    if (buffer[0] == 0xff && buffer[1] == 0xff && buffer[2] == 0xff && buffer[3] == 0xff)
                    {
                        // another sector to process?
                        if (dirEntry.EndPos() - iso.Position > 2048)
                        {
                            iso.Position += 2048L - (iso.Position % 2048L);
                            continue;
                        }
                        break;
                    }

                    // read sector
                    iso.Read(buffer, 0, 4);
                    sector = BitConverter.ToInt32(buffer, 0);
                    if (sector > 0)
                    {
                        sector -= offset;
                        byte[] corrected = BitConverter.GetBytes(sector);
                        iso.Position -= 4;
                        iso.Write(corrected, 0, 4);
                    }

                    // get size
                    iso.Read(buffer, 0, 4);
                    size = BitConverter.ToInt32(buffer, 0);

                    // get attributes
                    iso.Read(buffer, 0, 1);

                    // if directory add to list of tables to process
                    if ((buffer[0] & 0x10) == 0x10)
                    {
                        directories.Enqueue(new DirEntry(sector, size));
                    }

                    // get filename length
                    iso.Read(buffer, 0, 1);
                    // skip it
                    iso.Position += buffer[0];

                    // skip padding
                    if ((14 + buffer[0]) % 4 > 0)
                    {
                        iso.Position += 4 - ((14 + buffer[0]) % 4);
                    }
                }
            }
        }