Search
lxdream.org :: lxdream/src/drivers/cdrom/cd_gdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/cd_gdi.c
changeset 1298:d0eb2307b847
prev1296:30ecee61f811
author nkeynes
date Wed Feb 04 08:38:23 2015 +1000 (5 years ago)
permissions -rw-r--r--
last change Fix assorted compile warnings reported by Clang
file annotate diff log raw
nkeynes@492
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@492
     3
 *
nkeynes@492
     4
 * NullDC GDI image format
nkeynes@492
     5
 *
nkeynes@492
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@492
     7
 *
nkeynes@492
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@492
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@492
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@492
    11
 * (at your option) any later version.
nkeynes@492
    12
 *
nkeynes@492
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@492
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@492
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@492
    16
 * GNU General Public License for more details.
nkeynes@492
    17
 */
nkeynes@492
    18
nkeynes@492
    19
#include <stdlib.h>
nkeynes@492
    20
#include <stdio.h>
nkeynes@492
    21
#include <stdint.h>
nkeynes@759
    22
#include <string.h>
nkeynes@492
    23
#include <fcntl.h>
nkeynes@492
    24
#include <errno.h>
nkeynes@492
    25
#include <sys/stat.h>
nkeynes@1296
    26
#include <glib.h>
nkeynes@1097
    27
#include "drivers/cdrom/cdimpl.h"
nkeynes@492
    28
nkeynes@492
    29
nkeynes@492
    30
static gboolean gdi_image_is_valid( FILE *f );
nkeynes@1097
    31
static gboolean gdi_image_read_toc( cdrom_disc_t disc, ERROR *err );
nkeynes@492
    32
nkeynes@1097
    33
struct cdrom_disc_factory gdi_disc_factory = { "NullDC GD-Rom Image", "gdi",
nkeynes@1097
    34
        gdi_image_is_valid, NULL, gdi_image_read_toc };
nkeynes@492
    35
nkeynes@492
    36
static gboolean gdi_image_is_valid( FILE *f )
nkeynes@492
    37
{
nkeynes@492
    38
    char line[512];
nkeynes@492
    39
    uint32_t track_count;
nkeynes@736
    40
nkeynes@492
    41
    fseek(f, 0, SEEK_SET);
nkeynes@492
    42
    if( fgets( line, sizeof(line), f ) == NULL ) {
nkeynes@736
    43
        return FALSE;
nkeynes@492
    44
    }
nkeynes@492
    45
    track_count = strtoul(line, NULL, 0);
nkeynes@492
    46
    if( track_count == 0 || track_count > 99 ) {
nkeynes@736
    47
        return FALSE;
nkeynes@492
    48
    }
nkeynes@492
    49
    return TRUE;
nkeynes@492
    50
}
nkeynes@492
    51
nkeynes@1097
    52
static gboolean gdi_image_read_toc( cdrom_disc_t disc, ERROR *err )
nkeynes@492
    53
{
nkeynes@492
    54
    int i;
nkeynes@492
    55
    uint32_t track_count;
nkeynes@492
    56
    char line[512];
nkeynes@1097
    57
    int session = 1;
nkeynes@492
    58
    gchar *dirname;
nkeynes@492
    59
nkeynes@1097
    60
    FILE *f = cdrom_disc_get_base_file(disc);
nkeynes@1097
    61
nkeynes@492
    62
    fseek(f, 0, SEEK_SET);
nkeynes@736
    63
nkeynes@492
    64
    if( fgets( line, sizeof(line), f ) == NULL ) {
nkeynes@1109
    65
        SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image" );
nkeynes@736
    66
        return FALSE;
nkeynes@492
    67
    }
nkeynes@492
    68
    track_count = strtoul(line, NULL, 0);
nkeynes@492
    69
    if( track_count == 0 || track_count > 99 ) {
nkeynes@1109
    70
        SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image" );
nkeynes@1097
    71
        return FALSE;
nkeynes@492
    72
    }
nkeynes@492
    73
nkeynes@1097
    74
    dirname = g_path_get_dirname(disc->name);
nkeynes@1097
    75
    disc->disc_type = CDROM_DISC_GDROM;
nkeynes@1023
    76
    disc->track_count = track_count;
nkeynes@1097
    77
    disc->session_count = 2;
nkeynes@492
    78
    for( i=0; i<track_count; i++ ) {
nkeynes@736
    79
        int track_no, start_lba, flags, size, offset;
nkeynes@736
    80
        char filename[256];
nkeynes@492
    81
nkeynes@736
    82
        if( fgets( line, sizeof(line), f ) == NULL ) {
nkeynes@1097
    83
            cdrom_disc_unref(disc);
nkeynes@1109
    84
            SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image - unexpected end of file" );
nkeynes@1097
    85
            return FALSE;
nkeynes@736
    86
        }
nkeynes@736
    87
        sscanf( line, "%d %d %d %d %s %d", &track_no, &start_lba, &flags, &size,
nkeynes@736
    88
                filename, &offset );
nkeynes@736
    89
        if( start_lba >= 45000 ) {
nkeynes@1097
    90
            session = 2;
nkeynes@736
    91
        }
nkeynes@1097
    92
        disc->track[i].sessionno = session;
nkeynes@1097
    93
        disc->track[i].lba = start_lba;
nkeynes@1023
    94
        disc->track[i].flags = (flags & 0x0F)<<4;
nkeynes@1097
    95
nkeynes@1097
    96
        sector_mode_t mode;
nkeynes@1097
    97
        if( disc->track[i].flags & TRACK_FLAG_DATA ) {
nkeynes@1097
    98
            /* Data track */
nkeynes@1097
    99
            switch(size) {
nkeynes@1097
   100
            case 0:    mode = SECTOR_MODE2_FORM1; break; /* Default */
nkeynes@1097
   101
            case 2048: mode = SECTOR_MODE2_FORM1; break;
nkeynes@1097
   102
            case 2324: mode = SECTOR_MODE2_FORM2; break;
nkeynes@1097
   103
            case 2336: mode = SECTOR_SEMIRAW_MODE2; break;
nkeynes@1097
   104
            case 2352: mode = SECTOR_RAW_XA; break;
nkeynes@1097
   105
            default:
nkeynes@1109
   106
                SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid sector size '%d' in GDI track %d", size, (i+1) );
nkeynes@1097
   107
                g_free(dirname);
nkeynes@1097
   108
                return FALSE;
nkeynes@1097
   109
            }
nkeynes@1097
   110
        } else {
nkeynes@1097
   111
            /* Audio track */
nkeynes@1097
   112
            mode = SECTOR_CDDA;
nkeynes@1097
   113
            if( size == 0 )
nkeynes@1097
   114
                size = 2352;
nkeynes@1097
   115
            else if( size != 2352 ) {
nkeynes@1109
   116
                SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid sector size '%d' for audio track %d", size, (i+1) );
nkeynes@1097
   117
                g_free(dirname);
nkeynes@1097
   118
                return FALSE;
nkeynes@1097
   119
            }
nkeynes@1097
   120
        }
nkeynes@736
   121
        if( strcasecmp( filename, "none" ) == 0 ) {
nkeynes@1097
   122
            disc->track[i].source = null_sector_source_new( mode, 0 );
nkeynes@736
   123
        } else {
nkeynes@736
   124
            gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, filename );
nkeynes@1097
   125
            disc->track[i].source = file_sector_source_new_filename( pathname, mode,
nkeynes@1097
   126
                    offset, FILE_SECTOR_FULL_FILE );
nkeynes@736
   127
            g_free(pathname);
nkeynes@1097
   128
            if( disc->track[i].source == NULL ) {
nkeynes@1109
   129
                /* Note: status is invalid because it's a failure of the GDI file */
nkeynes@1109
   130
                SET_ERROR( err, LX_ERR_FILE_INVALID, "GDI track file '%s' could not be opened (%s)", filename, strerror(errno) );
nkeynes@736
   131
                g_free(dirname);
nkeynes@1097
   132
                return FALSE;
nkeynes@736
   133
            }
nkeynes@736
   134
        }
nkeynes@492
   135
    }
nkeynes@492
   136
    g_free(dirname);
nkeynes@1097
   137
    return TRUE;
nkeynes@492
   138
}
.