4 * NullDC GDI image format
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
26 #include <glib/gutils.h>
27 #include <glib/gstrfuncs.h>
28 #include "drivers/cdrom/cdimpl.h"
31 static gboolean gdi_image_is_valid( FILE *f );
32 static gboolean gdi_image_read_toc( cdrom_disc_t disc, ERROR *err );
34 struct cdrom_disc_factory gdi_disc_factory = { "NullDC GD-Rom Image", "gdi",
35 gdi_image_is_valid, NULL, gdi_image_read_toc };
37 static gboolean gdi_image_is_valid( FILE *f )
42 fseek(f, 0, SEEK_SET);
43 if( fgets( line, sizeof(line), f ) == NULL ) {
46 track_count = strtoul(line, NULL, 0);
47 if( track_count == 0 || track_count > 99 ) {
53 static gboolean gdi_image_read_toc( cdrom_disc_t disc, ERROR *err )
62 FILE *f = cdrom_disc_get_base_file(disc);
64 fseek(f, 0, SEEK_SET);
66 if( fgets( line, sizeof(line), f ) == NULL ) {
67 SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image" );
70 track_count = strtoul(line, NULL, 0);
71 if( track_count == 0 || track_count > 99 ) {
72 SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image" );
76 dirname = g_path_get_dirname(disc->name);
77 disc->disc_type = CDROM_DISC_GDROM;
78 disc->track_count = track_count;
79 disc->session_count = 2;
80 for( i=0; i<track_count; i++ ) {
81 int track_no, start_lba, flags, size, offset;
84 if( fgets( line, sizeof(line), f ) == NULL ) {
85 cdrom_disc_unref(disc);
86 SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image - unexpected end of file" );
89 sscanf( line, "%d %d %d %d %s %d", &track_no, &start_lba, &flags, &size,
91 if( start_lba >= 45000 ) {
94 disc->track[i].sessionno = session;
95 disc->track[i].lba = start_lba;
96 disc->track[i].flags = (flags & 0x0F)<<4;
99 if( disc->track[i].flags & TRACK_FLAG_DATA ) {
102 case 0: mode = SECTOR_MODE2_FORM1; break; /* Default */
103 case 2048: mode = SECTOR_MODE2_FORM1; break;
104 case 2324: mode = SECTOR_MODE2_FORM2; break;
105 case 2336: mode = SECTOR_SEMIRAW_MODE2; break;
106 case 2352: mode = SECTOR_RAW_XA; break;
108 SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid sector size '%d' in GDI track %d", size, (i+1) );
117 else if( size != 2352 ) {
118 SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid sector size '%d' for audio track %d", size, (i+1) );
123 if( strcasecmp( filename, "none" ) == 0 ) {
124 disc->track[i].source = null_sector_source_new( mode, 0 );
126 gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, filename );
127 disc->track[i].source = file_sector_source_new_filename( pathname, mode,
128 offset, FILE_SECTOR_FULL_FILE );
130 if( disc->track[i].source == NULL ) {
131 /* Note: status is invalid because it's a failure of the GDI file */
132 SET_ERROR( err, LX_ERR_FILE_INVALID, "GDI track file '%s' could not be opened (%s)", filename, strerror(errno) );
.