filename | src/drivers/cdrom/cd_gdi.c |
changeset | 1296:30ecee61f811 |
prev | 1286:8376a612a79d |
next | 1298:d0eb2307b847 |
author | nkeynes |
date | Sat Jan 26 14:00:48 2013 +1000 (11 years ago) |
permissions | -rw-r--r-- |
last change | Change glib includes to #include <glib.h> rather than the individual headers, as recent glib versions are breaking on this |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * NullDC GDI image format
5 *
6 * Copyright (c) 2005 Nathan Keynes.
7 *
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.
12 *
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.
17 */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26 #include <glib.h>
27 #include "drivers/cdrom/cdimpl.h"
30 static gboolean gdi_image_is_valid( FILE *f );
31 static gboolean gdi_image_read_toc( cdrom_disc_t disc, ERROR *err );
33 struct cdrom_disc_factory gdi_disc_factory = { "NullDC GD-Rom Image", "gdi",
34 gdi_image_is_valid, NULL, gdi_image_read_toc };
36 static gboolean gdi_image_is_valid( FILE *f )
37 {
38 char line[512];
39 uint32_t track_count;
41 fseek(f, 0, SEEK_SET);
42 if( fgets( line, sizeof(line), f ) == NULL ) {
43 return FALSE;
44 }
45 track_count = strtoul(line, NULL, 0);
46 if( track_count == 0 || track_count > 99 ) {
47 return FALSE;
48 }
49 return TRUE;
50 }
52 static gboolean gdi_image_read_toc( cdrom_disc_t disc, ERROR *err )
53 {
54 int i;
55 uint32_t track_count;
56 struct stat st;
57 char line[512];
58 int session = 1;
59 gchar *dirname;
61 FILE *f = cdrom_disc_get_base_file(disc);
63 fseek(f, 0, SEEK_SET);
65 if( fgets( line, sizeof(line), f ) == NULL ) {
66 SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image" );
67 return FALSE;
68 }
69 track_count = strtoul(line, NULL, 0);
70 if( track_count == 0 || track_count > 99 ) {
71 SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image" );
72 return FALSE;
73 }
75 dirname = g_path_get_dirname(disc->name);
76 disc->disc_type = CDROM_DISC_GDROM;
77 disc->track_count = track_count;
78 disc->session_count = 2;
79 for( i=0; i<track_count; i++ ) {
80 int track_no, start_lba, flags, size, offset;
81 char filename[256];
83 if( fgets( line, sizeof(line), f ) == NULL ) {
84 cdrom_disc_unref(disc);
85 SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image - unexpected end of file" );
86 return FALSE;
87 }
88 sscanf( line, "%d %d %d %d %s %d", &track_no, &start_lba, &flags, &size,
89 filename, &offset );
90 if( start_lba >= 45000 ) {
91 session = 2;
92 }
93 disc->track[i].sessionno = session;
94 disc->track[i].lba = start_lba;
95 disc->track[i].flags = (flags & 0x0F)<<4;
97 sector_mode_t mode;
98 if( disc->track[i].flags & TRACK_FLAG_DATA ) {
99 /* Data track */
100 switch(size) {
101 case 0: mode = SECTOR_MODE2_FORM1; break; /* Default */
102 case 2048: mode = SECTOR_MODE2_FORM1; break;
103 case 2324: mode = SECTOR_MODE2_FORM2; break;
104 case 2336: mode = SECTOR_SEMIRAW_MODE2; break;
105 case 2352: mode = SECTOR_RAW_XA; break;
106 default:
107 SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid sector size '%d' in GDI track %d", size, (i+1) );
108 g_free(dirname);
109 return FALSE;
110 }
111 } else {
112 /* Audio track */
113 mode = SECTOR_CDDA;
114 if( size == 0 )
115 size = 2352;
116 else if( size != 2352 ) {
117 SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid sector size '%d' for audio track %d", size, (i+1) );
118 g_free(dirname);
119 return FALSE;
120 }
121 }
122 if( strcasecmp( filename, "none" ) == 0 ) {
123 disc->track[i].source = null_sector_source_new( mode, 0 );
124 } else {
125 gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, filename );
126 disc->track[i].source = file_sector_source_new_filename( pathname, mode,
127 offset, FILE_SECTOR_FULL_FILE );
128 g_free(pathname);
129 if( disc->track[i].source == NULL ) {
130 /* Note: status is invalid because it's a failure of the GDI file */
131 SET_ERROR( err, LX_ERR_FILE_INVALID, "GDI track file '%s' could not be opened (%s)", filename, strerror(errno) );
132 g_free(dirname);
133 return FALSE;
134 }
135 }
136 }
137 g_free(dirname);
138 return TRUE;
139 }
.