psxinterpreter: reduce the use of globals
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
index 2dcaf49..be7300f 100644 (file)
@@ -680,6 +680,7 @@ static int handlepbp(const char *isofile) {
        off_t psisoimg_offs, cdimg_base;
        unsigned int t, cd_length;
        unsigned int offsettab[8];
+       unsigned int psar_offs, index_entry_size, index_entry_offset;
        const char *ext = NULL;
        int i, ret;
 
@@ -698,21 +699,23 @@ static int handlepbp(const char *isofile) {
                goto fail_io;
        }
 
-       ret = fseeko(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
+       psar_offs = SWAP32(pbp_hdr.psar_offs);
+
+       ret = fseeko(cdHandle, psar_offs, SEEK_SET);
        if (ret != 0) {
-               SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs);
+               SysPrintf("failed to seek to %x\n", psar_offs);
                goto fail_io;
        }
 
-       psisoimg_offs = pbp_hdr.psar_offs;
+       psisoimg_offs = psar_offs;
        if (fread(psar_sig, 1, sizeof(psar_sig), cdHandle) != sizeof(psar_sig))
                goto fail_io;
        psar_sig[10] = 0;
        if (strcmp(psar_sig, "PSTITLEIMG") == 0) {
                // multidisk image?
-               ret = fseeko(cdHandle, pbp_hdr.psar_offs + 0x200, SEEK_SET);
+               ret = fseeko(cdHandle, psar_offs + 0x200, SEEK_SET);
                if (ret != 0) {
-                       SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs + 0x200);
+                       SysPrintf("failed to seek to %x\n", psar_offs + 0x200);
                        goto fail_io;
                }
 
@@ -734,7 +737,7 @@ static int handlepbp(const char *isofile) {
                if (cdrIsoMultidiskSelect >= cdrIsoMultidiskCount)
                        cdrIsoMultidiskSelect = 0;
 
-               psisoimg_offs += offsettab[cdrIsoMultidiskSelect];
+               psisoimg_offs += SWAP32(offsettab[cdrIsoMultidiskSelect]);
 
                ret = fseeko(cdHandle, psisoimg_offs, SEEK_SET);
                if (ret != 0) {
@@ -818,12 +821,15 @@ static int handlepbp(const char *isofile) {
                        goto fail_index;
                }
 
-               if (index_entry.size == 0)
+               index_entry_size = SWAP32(index_entry.size);
+               index_entry_offset = SWAP32(index_entry.offset);
+
+               if (index_entry_size == 0)
                        break;
 
-               compr_img->index_table[i] = cdimg_base + index_entry.offset;
+               compr_img->index_table[i] = cdimg_base + index_entry_offset;
        }
-       compr_img->index_table[i] = cdimg_base + index_entry.offset + index_entry.size;
+       compr_img->index_table[i] = cdimg_base + index_entry_offset + index_entry_size;
 
        return 0;
 
@@ -931,7 +937,7 @@ fail_io:
 
 #ifdef HAVE_CHD
 static int handlechd(const char *isofile) {
-       int frame_offset = 0;
+       int frame_offset = 150;
        int file_offset = 0;
 
        chd_img = calloc(1, sizeof(*chd_img));
@@ -980,20 +986,26 @@ static int handlechd(const char *isofile) {
                else
                        break;
 
-               if(md.track == 1)
-                       md.pregap = 150;
-               else
-                       sec2msf(msf2sec(ti[md.track-1].length) + md.pregap, ti[md.track-1].length);
+               SysPrintf("chd: %s\n", meta);
+
+               if (md.track == 1) {
+                       if (!strncmp(md.subtype, "RW", 2)) {
+                               subChanMixed = TRUE;
+                               if (!strcmp(md.subtype, "RW_RAW"))
+                                       subChanRaw = TRUE;
+                       }
+               }
 
                ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA;
 
                sec2msf(frame_offset + md.pregap, ti[md.track].start);
                sec2msf(md.frames, ti[md.track].length);
 
-               ti[md.track].start_offset = file_offset;
+               ti[md.track].start_offset = file_offset + md.pregap;
 
-               frame_offset += md.pregap + md.frames + md.postgap;
-               file_offset += md.frames + md.postgap;
+               // XXX: what about postgap?
+               frame_offset += md.frames;
+               file_offset += md.frames;
                numtracks++;
        }
 
@@ -1204,6 +1216,12 @@ static int cdread_chd(FILE *f, unsigned int base, void *dest, int sector)
        if (dest != cdbuffer) // copy avoid HACK
                memcpy(dest, chd_img->buffer[chd_img->sector_in_hunk],
                        CD_FRAMESIZE_RAW);
+       if (subChanMixed) {
+               memcpy(subbuffer, chd_img->buffer[chd_img->sector_in_hunk] + CD_FRAMESIZE_RAW,
+                       SUB_FRAMESIZE);
+               if (subChanRaw)
+                       DecodeRawSubData();
+       }
        return CD_FRAMESIZE_RAW;
 }
 #endif
@@ -1254,7 +1272,8 @@ static long CALLBACK ISOopen(void) {
        boolean isMode1ISO = FALSE;
        char alt_bin_filename[MAXPATHLEN];
        const char *bin_filename;
-       char image_str[1024] = {0};
+       char image_str[1024];
+       int is_chd = 0;
 
        if (cdHandle != NULL) {
                return 0; // it's already open
@@ -1267,7 +1286,8 @@ static long CALLBACK ISOopen(void) {
                return -1;
        }
 
-       sprintf(image_str, "Loaded CD Image: %s", GetIsoFile());
+       snprintf(image_str, sizeof(image_str) - 6*4 - 1,
+               "Loaded CD Image: %s", GetIsoFile());
 
        cddaBigEndian = FALSE;
        subChanMixed = FALSE;
@@ -1306,6 +1326,7 @@ static long CALLBACK ISOopen(void) {
                strcat(image_str, "[+chd]");
                CDR_getBuffer = ISOgetBuffer_chd;
                cdimg_read_func = cdread_chd;
+               is_chd = 1;
        }
 #endif
 
@@ -1366,7 +1387,7 @@ static long CALLBACK ISOopen(void) {
 
        PrintTracks();
 
-       if (subChanMixed)
+       if (subChanMixed && !is_chd)
                cdimg_read_func = cdread_sub_mixed;
        else if (isMode1ISO)
                cdimg_read_func = cdread_2048;