Search
lxdream.org :: lxdream/src/gdrom/gdi.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdi.c
changeset 492:84e33e4dda1c
next498:10d5ba99a778
author nkeynes
date Tue Nov 06 08:35:16 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Issue #37: Add nulldc GDI format
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/gdrom/gdi.c Tue Nov 06 08:35:16 2007 +0000
1.3 @@ -0,0 +1,127 @@
1.4 +/**
1.5 + * $Id: gdi.c,v 1.1 2007-11-06 08:35:16 nkeynes Exp $
1.6 + *
1.7 + * NullDC GDI image format
1.8 + *
1.9 + * Copyright (c) 2005 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include <stdlib.h>
1.23 +#include <stdio.h>
1.24 +#include <stdint.h>
1.25 +#include <fcntl.h>
1.26 +#include <errno.h>
1.27 +#include <sys/stat.h>
1.28 +#include <glib/gutils.h>
1.29 +#include "gdrom/gdrom.h"
1.30 +
1.31 +
1.32 +static gboolean gdi_image_is_valid( FILE *f );
1.33 +static gdrom_disc_t gdi_image_open( const gchar *filename, FILE *f );
1.34 +
1.35 +struct gdrom_image_class gdi_image_class = { "NullDC GD-Rom Image", "gdi",
1.36 + gdi_image_is_valid, gdi_image_open };
1.37 +
1.38 +static gboolean gdi_image_is_valid( FILE *f )
1.39 +{
1.40 + char line[512];
1.41 + uint32_t track_count;
1.42 +
1.43 + fseek(f, 0, SEEK_SET);
1.44 + if( fgets( line, sizeof(line), f ) == NULL ) {
1.45 + return FALSE;
1.46 + }
1.47 + track_count = strtoul(line, NULL, 0);
1.48 + if( track_count == 0 || track_count > 99 ) {
1.49 + return FALSE;
1.50 + }
1.51 + return TRUE;
1.52 +}
1.53 +
1.54 +static gdrom_disc_t gdi_image_open( const gchar *filename, FILE *f )
1.55 +{
1.56 + int i;
1.57 + uint32_t track_count;
1.58 + gdrom_disc_t disc;
1.59 + gdrom_image_t image;
1.60 + struct stat st;
1.61 + char line[512];
1.62 + gchar *dirname;
1.63 +
1.64 + fseek(f, 0, SEEK_SET);
1.65 +
1.66 + if( fgets( line, sizeof(line), f ) == NULL ) {
1.67 + return FALSE;
1.68 + }
1.69 + track_count = strtoul(line, NULL, 0);
1.70 + if( track_count == 0 || track_count > 99 ) {
1.71 + return NULL;
1.72 + }
1.73 +
1.74 + disc = gdrom_image_new(filename, f);
1.75 + if( disc == NULL ) {
1.76 + ERROR("Unable to allocate memory!");
1.77 + return NULL;
1.78 + }
1.79 + dirname = g_path_get_dirname(filename);
1.80 + image = (gdrom_image_t)disc;
1.81 + image->disc_type = IDE_DISC_GDROM;
1.82 + image->track_count = track_count;
1.83 + for( i=0; i<track_count; i++ ) {
1.84 + int track_no, start_lba, flags, size, offset;
1.85 + char filename[256];
1.86 +
1.87 + if( fgets( line, sizeof(line), f ) == NULL ) {
1.88 + gdrom_image_destroy_no_close(disc);
1.89 + return NULL;
1.90 + }
1.91 + sscanf( line, "%d %d %d %d %s %d", &track_no, &start_lba, &flags, &size,
1.92 + &filename, &offset );
1.93 + if( start_lba >= 45000 ) {
1.94 + image->track[i].session = 1;
1.95 + } else {
1.96 + image->track[i].session = 0;
1.97 + }
1.98 + image->track[i].lba = start_lba + 150; // 2-second offset
1.99 + image->track[i].flags = (flags & 0x0F)<<4;
1.100 + image->track[i].sector_size = size;
1.101 + if( strcasecmp( filename, "none" ) == 0 ) {
1.102 + image->track[i].file = NULL;
1.103 + image->track[i].sector_count = 0;
1.104 + image->track[i].mode = GDROM_MODE1;
1.105 + } else {
1.106 + gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, filename );
1.107 + image->track[i].file = fopen( pathname, "ro" );
1.108 + g_free(pathname);
1.109 + if( image->track[i].file == NULL ) {
1.110 + gdrom_image_destroy_no_close(disc);
1.111 + g_free(dirname);
1.112 + return NULL;
1.113 + }
1.114 + fstat( fileno(image->track[i].file), &st );
1.115 + image->track[i].sector_count = st.st_size / size;
1.116 + switch(size) {
1.117 + case 2048: image->track[i].mode = GDROM_MODE1; break;
1.118 + case 2336: image->track[i].mode = GDROM_GD; break;
1.119 + case 2352: image->track[i].mode = GDROM_CDDA; break;
1.120 + default:
1.121 + gdrom_image_destroy_no_close(disc);
1.122 + g_free(dirname);
1.123 + return NULL;
1.124 + }
1.125 + }
1.126 + image->track[i].offset = offset;
1.127 + }
1.128 + g_free(dirname);
1.129 + return disc;
1.130 +}
.