Search
lxdream.org :: lxdream/src/gdrom/nrg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/nrg.c
changeset 464:8e099fad42a6
prev422:61a0598e07ff
next468:3a49695e081a
author nkeynes
date Sat Oct 27 05:44:54 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Initial gdrom attachment menu
view annotate diff log raw
     1 /**
     2  * $Id: nrg.c,v 1.6 2007-10-27 05:44:54 nkeynes Exp $
     3  *
     4  * Nero (NRG) CD file format. File information stolen shamelessly from
     5  * libcdio.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <assert.h>
    21 #include <stdio.h>
    22 #include <errno.h>
    23 #include <netinet/in.h>
    24 #include "gdrom/gdrom.h"
    25 #include "dream.h"
    27 static gboolean nrg_image_is_valid( FILE *f );
    28 static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f );
    30 struct gdrom_image_class nrg_image_class = { "Nero", "nrg", 
    31 					     nrg_image_is_valid, nrg_image_open };
    33 #define NERO_V55_ID  0x4e455235 
    34 #define NERO_V50_ID  0x4e45524f 
    36 /* Courtesy of libcdio */
    37 /* 5.0 or earlier */
    38 #define NERO_ID  0x4e45524f  /* Nero pre 5.5.x */
    39 #define CUES_ID  0x43554553  /* Nero pre version 5.5.x-6.x */
    40 #define DAOI_ID  0x44414f49
    41 #define ETNF_ID  0x45544e46
    42 #define SINF_ID  0x53494e46  /* Session information */
    43 #define END_ID  0x454e4421
    44 /* 5.5+ only */
    45 #define NER5_ID  0x4e455235  /* Nero version 5.5.x */
    46 #define CDTX_ID  0x43445458  /* CD TEXT */
    47 #define CUEX_ID  0x43554558  /* Nero version 5.5.x-6.x */
    48 #define DAOX_ID  0x44414f58  /* Nero version 5.5.x-6.x */
    49 #define ETN2_ID  0x45544e32
    50 #define MTYP_ID  0x4d545950  /* Disc Media type? */
    53 union nrg_footer {
    54     struct nrg_footer_v50 {
    55 	uint32_t dummy;
    56 	uint32_t id;
    57 	uint32_t offset;
    58     } v50;
    59     struct nrg_footer_v55 {
    60 	uint32_t id;
    61 	uint64_t offset;
    62     } v55;
    63 };
    65 struct nrg_chunk {
    66     uint32_t id;
    67     uint32_t length;
    68 };
    70 struct nrg_etnf {
    71     uint32_t offset;
    72     uint32_t length;
    73     uint32_t mode;
    74     uint32_t lba;
    75     uint32_t padding;
    76 };
    78 struct nrg_cues {
    79     uint8_t type;
    80     uint8_t track;
    81     uint8_t control;
    82     uint8_t pad;
    83     uint32_t addr;
    84 };
    86 struct nrg_daoi {
    87     uint32_t length;
    88     char mcn[14];
    89     uint8_t disc_mode;
    90     uint8_t unknown[2]; /* always 01 01? */
    91     uint8_t track_count;
    92     struct nrg_daoi_track {
    93 	char unknown[10];
    94 	uint32_t sector_size __attribute__((packed)); /* Always 0? */
    95 	uint8_t mode;
    96 	uint8_t unknown2[3]; /* Always 00 00 01? */
    97 	uint32_t pregap __attribute__((packed));
    98 	uint32_t offset __attribute__((packed));
    99 	uint32_t end __attribute__((packed));
   100     } track[0];
   101 } __attribute__((packed));
   103 /**
   104  * Convert an 8-bit BCD number to normal integer form. 
   105  * Eg, 0x79 => 79
   106  */
   107 uint8_t static bcd_to_uint8( uint8_t bcd )
   108 {
   109     return (bcd & 0x0F) + (((bcd & 0xF0)>>4)*10);
   110 }
   113 /**
   114  * Convert a 32 bit MSF address (BCD coded) to the
   115  * equivalent LBA form. 
   116  * Eg, 0x
   117  */
   118 uint32_t static msf_to_lba( uint32_t msf )
   119 {
   120     msf = ntohl(msf);
   121     int f = bcd_to_uint8(msf);
   122     int s = bcd_to_uint8(msf>>8);
   123     int m = bcd_to_uint8(msf>>16);
   124     return (m * 60 + s) * 75 + f;
   126 }
   128 uint32_t static nrg_track_mode( uint8_t mode )
   129 {
   130     switch( mode ) {
   131     case 0: return GDROM_MODE1;
   132     case 2: return GDROM_MODE2_XA1;
   133     case 3: return GDROM_MODE2;
   134     case 7: return GDROM_CDDA;
   135     default: 
   136 	ERROR( "Unrecognized track mode %d in Nero image", mode );
   137 	return -1;
   138     }
   139 }
   141 static gboolean nrg_image_is_valid( FILE *f )
   142 {
   143     union nrg_footer footer;
   145     fseek( f, -12, SEEK_END );
   146     fread( &footer, sizeof(footer), 1, f );
   147     if( ntohl(footer.v50.id) == NERO_V50_ID ) {
   148 	return TRUE;
   149     } else {
   150 	return FALSE;
   151     }
   152 }
   154 static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f )
   155 {
   156     union nrg_footer footer;
   157     struct nrg_chunk chunk;
   158     struct nrg_daoi *dao;
   159     gdrom_disc_t disc;
   160     gdrom_image_t image;
   161     gboolean end = FALSE;
   162     int session_id = 0;
   163     int session_track_id = 0;
   164     int track_id = 0;
   165     int cue_track_id = 0, cue_track_count = 0;
   166     int i;
   168     fseek( f, -12, SEEK_END );
   169     fread( &footer, sizeof(footer), 1, f );
   170     if( ntohl(footer.v50.id) == NERO_V50_ID ) {
   171 	INFO( "Loading Nero 5.0 image" );
   172 	fseek( f, ntohl(footer.v50.offset), SEEK_SET );
   173     } else if( ntohl(footer.v55.id) == NERO_V55_ID ) {
   174 	INFO( "Loading Nero 5.5+ image" );
   175 	fseek( f, ntohl(footer.v55.offset), SEEK_SET );
   176     } else {
   177 	/* Not a (recognized) Nero image */
   178 	return NULL;
   179     }
   181     disc = gdrom_image_new(filename, f);
   182     if( disc == NULL ) {
   183 	fclose(f);
   184 	ERROR("Unable to allocate memory!");
   185 	return NULL;
   186     }
   187     image = (gdrom_image_t)disc;
   189     do {
   190 	fread( &chunk, sizeof(chunk), 1, f );
   191 	chunk.length = ntohl(chunk.length);
   192 	char data[chunk.length];
   193 	fread( data, chunk.length, 1, f );
   194 	switch( ntohl(chunk.id) ) {
   195 	case CUES_ID:
   196 	    cue_track_id = track_id;
   197 	    cue_track_count = ((chunk.length / sizeof(struct nrg_cues)) >> 1) - 1;
   198 	    track_id += cue_track_count;
   199 	    for( i=0; i<chunk.length; i+= sizeof(struct nrg_cues) ) {
   200 		struct nrg_cues *cue = (struct nrg_cues *)(data+i);
   201 		int track = 0;
   202 		if( cue->track == 0 )
   203 		    continue; /* Track 0. Leadin? always 0? */
   204 		if( cue->track == 0xAA ) { /* end of disc */
   205 		    image->track[track_id-1].sector_count =
   206 			msf_to_lba( cue->addr ) - image->track[track_id-1].lba;
   207 		} else {
   208 		    track = cue_track_id + bcd_to_uint8(cue->track) - 1;
   209 		    if( (cue->control & 0x01) == 0 ) { 
   210 			/* Pre-gap address. */
   211 			if( track != 0 ) {
   212 			    image->track[track-1].sector_count = 
   213 				msf_to_lba( cue->addr ) - image->track[track-1].lba;
   214 			}
   215 		    } else { /* Track-start address */
   216 			image->track[track].lba = msf_to_lba( cue->addr );
   217 			image->track[track].flags = cue->type;
   218 		    }
   219 		}
   220 	    }
   221 	    break;
   222 	case DAOI_ID:
   223 	    dao = (struct nrg_daoi *)data;
   224 	    memcpy( image->mcn, dao->mcn, 13 );
   225 	    image->mcn[13] = '\0';
   226 	    assert( dao->track_count * 30 + 22 == chunk.length );
   227 	    assert( dao->track_count == cue_track_count );
   228 	    for( i=0; i<dao->track_count; i++ ) {
   229 		image->track[cue_track_id].sector_size = ntohl(dao->track[i].sector_size);
   230 		image->track[cue_track_id].offset = ntohl(dao->track[i].offset);
   231 		image->track[cue_track_id].mode = nrg_track_mode( dao->track[i].mode );
   232 		image->track[cue_track_id].sector_count =
   233 		    (ntohl(dao->track[i].end) - ntohl(dao->track[i].offset))/
   234 		    ntohl(dao->track[i].sector_size);
   235 		cue_track_id++;
   236 	    }
   237 	    break;
   238 	case SINF_ID: 
   239 	    /* Data is a single 32-bit number representing number of tracks in session */
   240 	    i = ntohl( *(uint32_t *)data );
   241 	    while( i-- > 0 )
   242 		image->track[session_track_id++].session = session_id;
   243 	    session_id++;
   244 	    break;
   245 	case ETNF_ID:
   246 	    for( i=0; i < chunk.length; i+= 0x14 ) {
   247 		struct nrg_etnf *etnf = (struct nrg_etnf *)(data+i);
   248 		image->track[track_id].offset = ntohl(etnf->offset);
   249 		image->track[track_id].lba = ntohl(etnf->lba) + (i+1)*GDROM_PREGAP;
   250 		image->track[track_id].mode = nrg_track_mode( ntohl(etnf->mode) );
   251 		if( image->track[track_id].mode == -1 ) {
   252 		    disc->close(disc);
   253 		    return NULL;
   254 		}
   255 		if( image->track[track_id].mode == GDROM_CDDA )
   256 		    image->track[track_id].flags = 0x01;
   257 		else
   258 		    image->track[track_id].flags = 0x01 | TRACK_DATA;
   259 		image->track[track_id].sector_size = GDROM_SECTOR_SIZE(image->track[track_id].mode);
   260 		image->track[track_id].sector_count = ntohl(etnf->length) / 
   261 		    image->track[track_id].sector_size;
   262 		track_id++;
   263 	    }
   264 	    break;
   265 	case END_ID:
   266 	    end = TRUE;
   267 	    break;
   268 	}
   269     } while( !end );
   270     image->track_count = track_id;
   271     return disc;
   272 }
.