0
Vote

Auto detection image format

description

Add a class Helper wich provides a method to the auto dectection image format.

comments

wrote Dec 9, 2007 at 8:45 PM

Format iso et bin tant que d'autres formats ne soient supportés.

wrote Dec 10, 2007 at 11:29 AM

wrote Dec 10, 2007 at 11:58 AM

wrote Feb 13, 2013 at 7:54 PM

Danixu86 wrote May 2, 2016 at 10:05 AM

I've got problems with ISO images converted from PSX (BIN_MODE2) games and after a lot of searches i've made a way to detect if the image is an ISO, BIN MODE1 or BIN MODE2. I'm new in C# then i know that maybe is not the best way:
private ImageFileFormat ImageFormatSelector(string filename)
{
    string MagicBytes = "CD001";
    byte[] ImageBytes = new byte[5];

    try
    {
        using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
        {
            fs.Seek(0x8001, SeekOrigin.Begin);
            fs.Read(ImageBytes, 0, ImageBytes.Length);

            // Check if image is ISO
            if (System.Text.Encoding.UTF8.GetString(ImageBytes) == MagicBytes)
            {
                System.Diagnostics.Debug.WriteLine("ISO Image detected...");
                return ImageFileFormat.ISO;
            }

            // Check if image is BIN Mode 1
            fs.Seek(0x9311, SeekOrigin.Begin);
            fs.Read(ImageBytes, 0, ImageBytes.Length);

            if (System.Text.Encoding.UTF8.GetString(ImageBytes) == MagicBytes)
            {
                System.Diagnostics.Debug.WriteLine("BIM Mode 1 Image detected...");
                return ImageFileFormat.BIN_Mode1;
            }

            // Check if image is BIN Mode 2
            fs.Seek(0x9319, SeekOrigin.Begin);
            fs.Read(ImageBytes, 0, ImageBytes.Length);

            if (System.Text.Encoding.UTF8.GetString(ImageBytes) == MagicBytes)
            {
                System.Diagnostics.Debug.WriteLine("BIN Mode 2 Image detected...");
                return ImageFileFormat.BIN_Mode2_Form1;
            }
        }
    }
For now i've done tests extracting a file from some PSX Images:
  • ISO
  • BIN/CUE
  • IMG/CCD/SUB
Works fine with all, but i've detected a bug in GomuIso library getting the output filename (at least with PSX games). It gets the right filenames using "iso.GetFiles();" and also extract the image using that filename (iso.ExtractFile(@"\SYSTEM.CNF", @"D:\")), but the output file includes the file attribute in filename (SYSTEM.CNF;1). It happens in all PSX images (BIN MODE2), but on some PSP games i've tested (ISO format) seems to work fine.

I've done some test with normal images too created with UltraIso, and i was able to extract the filelist from all using the above function. All images were ISO (ISO images) and BIN MODE1 the other images (IMG, BIN, MDF). I don't have more images to test, then maybe some images will fail...

I've also done some tests with PSP ISOs and PS2 ISO, but all are ISO format.

Greetings!!

Danixu86 wrote May 2, 2016 at 10:27 AM

Sorry, i forgot to copy a part of the end of above function:
private ImageFileFormat ImageFormatSelector(string filename)
{
    string MagicBytes = "CD001";
    byte[] ImageBytes = new byte[5];

    try
    {
        using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
        {
            fs.Seek(0x8001, SeekOrigin.Begin);
            fs.Read(ImageBytes, 0, ImageBytes.Length);

            // Check if image is ISO
            if (System.Text.Encoding.UTF8.GetString(ImageBytes) == MagicBytes)
            {
                System.Diagnostics.Debug.WriteLine("ISO Image detected...");
                return ImageFileFormat.ISO;
            }

            // Check if image is BIN Mode 1
            fs.Seek(0x9311, SeekOrigin.Begin);
            fs.Read(ImageBytes, 0, ImageBytes.Length);

            if (System.Text.Encoding.UTF8.GetString(ImageBytes) == MagicBytes)
            {
                System.Diagnostics.Debug.WriteLine("BIM Mode 1 Image detected...");
                return ImageFileFormat.BIN_Mode1;
            }

            // Check if image is BIN Mode 2
            fs.Seek(0x9319, SeekOrigin.Begin);
            fs.Read(ImageBytes, 0, ImageBytes.Length);

            if (System.Text.Encoding.UTF8.GetString(ImageBytes) == MagicBytes)
            {
                System.Diagnostics.Debug.WriteLine("BIN Mode 2 Image detected...");
                return ImageFileFormat.BIN_Mode2_Form1;
            }
        }
    }

    catch(Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("Error: " + ex.ToString());
        return ImageFileFormat.UNKNOWN;
    }
    return ImageFileFormat.UNKNOWN;
}