2 * $Id: cdi.c,v 1.7 2007-10-06 08:58:00 nkeynes Exp $
4 * CDI CD-image file support
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.
25 #include "gdrom/gdrom.h"
27 #define CDI_V2_ID 0x80000004
28 #define CDI_V3_ID 0x80000005
29 #define CDI_V35_ID 0x80000006
32 static gboolean cdi_image_is_valid( FILE *f );
33 static gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f );
35 struct gdrom_image_class cdi_image_class = { "DiscJuggler", "cdi",
36 cdi_image_is_valid, cdi_image_open };
38 static const char TRACK_START_MARKER[20] = { 0,0,1,0,0,0,255,255,255,255,
39 0,0,1,0,0,0,255,255,255,255 };
40 static const char EXT_MARKER[9] = {0,255,255,255,255,255,255,255,255 };
44 uint32_t header_offset;
47 struct cdi_track_data {
49 uint32_t pregap_length;
55 uint32_t total_length;
59 } __attribute__((packed));
61 gboolean cdi_image_is_valid( FILE *f )
64 struct cdi_trailer trail;
66 fseek( f, -8, SEEK_END );
68 fread( &trail, sizeof(trail), 1, f );
69 if( trail.header_offset >= len ||
70 trail.header_offset == 0 )
72 return trail.cdi_version == CDI_V2_ID || trail.cdi_version == CDI_V3_ID;
75 gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f )
77 gdrom_disc_t disc = NULL;
80 uint16_t session_count;
85 struct cdi_trailer trail;
88 fseek( f, -8, SEEK_END );
90 fread( &trail, sizeof(trail), 1, f );
91 if( trail.header_offset >= len ||
92 trail.header_offset == 0 )
95 if( trail.cdi_version == CDI_V2_ID ) trail.cdi_version = 2;
96 else if( trail.cdi_version == CDI_V3_ID ) trail.cdi_version = 3;
99 fseek( f, trail.header_offset, SEEK_SET );
100 fread( &session_count, sizeof(session_count), 1, f );
102 disc = gdrom_image_new(f);
105 ERROR("Unable to allocate memory!");
108 image = (gdrom_image_t)disc;
110 for( i=0; i< session_count; i++ ) {
111 fread( &track_count, sizeof(track_count), 1, f );
112 if( track_count + total_tracks > 99 ) {
113 ERROR( "Invalid number of tracks, bad cdi image\n" );
117 for( j=0; j<track_count; j++ ) {
118 struct cdi_track_data trk;
119 uint32_t new_fmt = 0;
120 uint8_t fnamelen = 0;
121 fread( &new_fmt, sizeof(new_fmt), 1, f );
122 if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
123 fseek( f, 8, SEEK_CUR ); /* Skip */
125 fread( marker, 20, 1, f );
126 if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
127 ERROR( "Track start marker not found, error reading cdi image\n" );
131 fseek( f, 4, SEEK_CUR );
132 fread( &fnamelen, 1, 1, f );
133 fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
134 fread( &trk, sizeof(trk), 1, f );
135 image->track[total_tracks].session = i;
136 image->track[total_tracks].lba = trk.start_lba + 150;
137 image->track[total_tracks].sector_count = trk.length;
140 image->track[total_tracks].mode = GDROM_CDDA;
141 image->track[total_tracks].sector_size = 2352;
142 image->track[total_tracks].flags = 0x01;
143 if( trk.sector_size != 2 ) {
144 ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
150 image->track[total_tracks].mode = GDROM_MODE1;
151 image->track[total_tracks].sector_size = 2048;
152 image->track[total_tracks].flags = 0x41;
153 if( trk.sector_size != 0 ) {
154 ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
160 image->track[total_tracks].flags = 0x41;
161 switch( trk.sector_size ) {
163 image->track[total_tracks].mode = GDROM_MODE2_XA1;
164 image->track[total_tracks].sector_size = 2048;
167 image->track[total_tracks].mode = GDROM_MODE2;
168 image->track[total_tracks].sector_size = 2336;
172 ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
178 ERROR( "Unsupported track mode %d", trk.mode );
182 image->track[total_tracks].offset = posn +
183 trk.pregap_length * image->track[total_tracks].sector_size ;
184 posn += trk.total_length * image->track[total_tracks].sector_size;
186 fread( marker, 1, 9, f );
187 if( memcmp( marker, EXT_MARKER, 9 ) == 0 ) {
188 fseek( f, 91, SEEK_CUR );
190 fseek( f, 3, SEEK_CUR );
194 image->track_count = total_tracks;
195 image->filename = filename;
.