Search
lxdream.org :: lxdream/src/gdrom/gdimage.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 492:84e33e4dda1c
prev480:d28c2992f5ee
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 --- a/src/gdrom/gdimage.c Wed Oct 31 11:53:35 2007 +0000
1.2 +++ b/src/gdrom/gdimage.c Tue Nov 06 08:35:16 2007 +0000
1.3 @@ -1,5 +1,5 @@
1.4 /**
1.5 - * $Id: gdimage.c,v 1.5 2007-10-31 11:53:35 nkeynes Exp $
1.6 + * $Id: gdimage.c,v 1.6 2007-11-06 08:35:16 nkeynes Exp $
1.7 *
1.8 * GD-Rom image-file common functions.
1.9 *
1.10 @@ -68,11 +68,20 @@
1.11
1.12 static void gdrom_image_destroy( gdrom_disc_t disc )
1.13 {
1.14 + int i;
1.15 + FILE *lastfile = NULL;
1.16 gdrom_image_t img = (gdrom_image_t)disc;
1.17 if( img->file != NULL ) {
1.18 fclose(img->file);
1.19 img->file = NULL;
1.20 }
1.21 + for( i=0; i<img->track_count; i++ ) {
1.22 + if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
1.23 + lastfile = img->track[i].file;
1.24 + fclose(lastfile);
1.25 + img->track[i].file = NULL;
1.26 + }
1.27 + }
1.28 if( disc->name != NULL ) {
1.29 g_free( (gpointer)disc->name );
1.30 disc->name = NULL;
1.31 @@ -82,10 +91,19 @@
1.32
1.33 void gdrom_image_destroy_no_close( gdrom_disc_t disc )
1.34 {
1.35 + int i;
1.36 + FILE *lastfile = NULL;
1.37 gdrom_image_t img = (gdrom_image_t)disc;
1.38 if( img->file != NULL ) {
1.39 img->file = NULL;
1.40 }
1.41 + for( i=0; i<img->track_count; i++ ) {
1.42 + if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
1.43 + lastfile = img->track[i].file;
1.44 + fclose(lastfile);
1.45 + img->track[i].file = NULL;
1.46 + }
1.47 + }
1.48 if( disc->name != NULL ) {
1.49 g_free( (gpointer)disc->name );
1.50 disc->name = NULL;
1.51 @@ -105,11 +123,33 @@
1.52 return -1;
1.53 }
1.54
1.55 +/**
1.56 + * Read a block from an image file, handling negative file offsets
1.57 + * with 0-fill.
1.58 + */
1.59 +static void gdrom_read_block( char *buf, int file_offset, int length, FILE *f )
1.60 +{
1.61 + if( file_offset < 0 ) {
1.62 + int size = -file_offset;
1.63 + if( size >= length ) {
1.64 + memset( buf, 0, length );
1.65 + return;
1.66 + } else {
1.67 + memset( buf, 0, size );
1.68 + file_offset = 0;
1.69 + length -= size;
1.70 + }
1.71 + }
1.72 + fseek( f, file_offset, SEEK_SET );
1.73 + fread( buf, length, 1, f );
1.74 +}
1.75 +
1.76 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
1.77 int mode, unsigned char *buf, uint32_t *length )
1.78 {
1.79 gdrom_image_t image = (gdrom_image_t)disc;
1.80 int file_offset, read_len, track_no;
1.81 + FILE *f;
1.82
1.83 track_no = gdrom_image_get_track_by_lba( image, lba );
1.84 if( track_no == -1 ) {
1.85 @@ -118,6 +158,13 @@
1.86 struct gdrom_track *track = &image->track[track_no-1];
1.87 file_offset = track->offset + track->sector_size * (lba - track->lba);
1.88 read_len = track->sector_size;
1.89 + if( track->file != NULL ) {
1.90 + f = track->file;
1.91 + } else {
1.92 + f = image->file;
1.93 + }
1.94 +
1.95 +
1.96
1.97 switch( mode ) {
1.98 case 0x24:
1.99 @@ -125,14 +172,12 @@
1.100 switch( track->mode ) {
1.101 case GDROM_MODE1:
1.102 case GDROM_MODE2_XA1:
1.103 - fseek( image->file, file_offset, SEEK_SET );
1.104 - fread( buf, track->sector_size, 1, image->file );
1.105 + gdrom_read_block( buf, file_offset, track->sector_size, f );
1.106 break;
1.107 case GDROM_MODE2:
1.108 read_len = 2048;
1.109 file_offset += 8; /* skip the subheader */
1.110 - fseek( image->file, file_offset, SEEK_SET );
1.111 - fread( buf, 2048, 1, image->file );
1.112 + gdrom_read_block( buf, file_offset, 2048, f );
1.113 break;
1.114 default:
1.115 return PKT_ERR_BADREADMODE;
.