Add simple (non-mode2) read/seek.
_cdio_generic.c: place to save common driver routines add cdio_get_track_sec_count.
This commit is contained in:
137
lib/_cdio_nrg.c
137
lib/_cdio_nrg.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
$Id: _cdio_nrg.c,v 1.1 2003/03/24 19:01:09 rocky Exp $
|
||||
$Id: _cdio_nrg.c,v 1.2 2003/03/29 17:32:00 rocky Exp $
|
||||
|
||||
Copyright (C) 2001,2003 Herbert Valerio Riedel <hvr@gnu.org>
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
#include "util.h"
|
||||
#include "_cdio_stdio.h"
|
||||
|
||||
static const char _rcsid[] = "$Id: _cdio_nrg.c,v 1.1 2003/03/24 19:01:09 rocky Exp $";
|
||||
static const char _rcsid[] = "$Id: _cdio_nrg.c,v 1.2 2003/03/29 17:32:00 rocky Exp $";
|
||||
|
||||
/* structures used */
|
||||
|
||||
@@ -102,9 +102,9 @@ typedef struct {
|
||||
int track_num; /* Probably is index+1 */
|
||||
msf_t start_msf;
|
||||
int start_index;
|
||||
int secsize; /* Number of sectors in track. Does not
|
||||
include pregap before next entry. */
|
||||
int flags; /* "DCP", "4CH", "PRE" */
|
||||
int sec_count; /* Number of sectors in track. Does not
|
||||
include pregap before next entry. */
|
||||
int flags; /* "DCP", "4CH", "PRE" */
|
||||
track_format_t track_format;
|
||||
bool track_green;
|
||||
} track_info_t;
|
||||
@@ -114,15 +114,15 @@ typedef struct {
|
||||
Possibly redundant with above track_info_t */
|
||||
typedef struct {
|
||||
uint32_t start_lsn;
|
||||
uint32_t secsize; /* Number of sectors in track. Does not
|
||||
include pregap before next entry. */
|
||||
uint64_t img_offset; /* Bytes offset from beginning of disk image file.*/
|
||||
uint32_t sec_count; /* Number of sectors in track. Does not
|
||||
include pregap before next entry. */
|
||||
uint64_t img_offset; /* Bytes offset from beginning of disk image file.*/
|
||||
} _mapping_t;
|
||||
|
||||
|
||||
typedef struct {
|
||||
bool sector_2336_flag;
|
||||
CdioDataSource *nrg_src;
|
||||
CdioDataSource *data_source;
|
||||
char *source_name;
|
||||
track_info_t tocent[100]; /* entry info for each track */
|
||||
track_t total_tracks; /* number of tracks in image */
|
||||
@@ -145,8 +145,8 @@ _cdio_free (void *user_data)
|
||||
|
||||
free(_obj->source_name);
|
||||
|
||||
if (_obj->nrg_src)
|
||||
cdio_stream_destroy (_obj->nrg_src);
|
||||
if (_obj->data_source)
|
||||
cdio_stream_destroy (_obj->data_source);
|
||||
|
||||
_cdio_list_free (_obj->mapping, true);
|
||||
|
||||
@@ -157,7 +157,7 @@ _cdio_free (void *user_data)
|
||||
simulate ioctl(CDROMREADTOCENTRY).
|
||||
*/
|
||||
static void
|
||||
_register_mapping (_img_private_t *_obj, lsn_t start_lsn, uint32_t secsize,
|
||||
_register_mapping (_img_private_t *_obj, lsn_t start_lsn, uint32_t sec_count,
|
||||
uint64_t img_offset, uint32_t blocksize)
|
||||
{
|
||||
const int track_num=_obj->total_tracks;
|
||||
@@ -167,11 +167,11 @@ _register_mapping (_img_private_t *_obj, lsn_t start_lsn, uint32_t secsize,
|
||||
_obj->mapping = _cdio_list_new ();
|
||||
|
||||
_cdio_list_append (_obj->mapping, _map);
|
||||
_map->start_lsn = start_lsn;
|
||||
_map->secsize = secsize;
|
||||
_map->start_lsn = start_lsn;
|
||||
_map->sec_count = sec_count;
|
||||
_map->img_offset = img_offset;
|
||||
|
||||
_obj->size = MAX (_obj->size, (start_lsn + secsize));
|
||||
_obj->size = MAX (_obj->size, (start_lsn + sec_count));
|
||||
|
||||
/* Update _obj->tocent[track_num] and track_num These structures are
|
||||
in a sense redundant witht the obj->mapping list. Perhaps one
|
||||
@@ -183,8 +183,7 @@ _register_mapping (_img_private_t *_obj, lsn_t start_lsn, uint32_t secsize,
|
||||
|
||||
_obj->tocent[track_num].track_num = track_num+1;
|
||||
_obj->tocent[track_num].blocksize = blocksize;
|
||||
_obj->tocent[track_num].secsize = secsize;
|
||||
_obj->tocent[track_num].secsize = secsize;
|
||||
_obj->tocent[track_num].sec_count = sec_count;
|
||||
|
||||
_obj->total_tracks++;
|
||||
|
||||
@@ -195,7 +194,7 @@ _register_mapping (_img_private_t *_obj, lsn_t start_lsn, uint32_t secsize,
|
||||
_obj->tocent[track_num].track_green = true;
|
||||
|
||||
|
||||
/* cdio_debug ("map: %d +%d -> %ld", start_lsn, secsize, img_offset); */
|
||||
/* cdio_debug ("map: %d +%d -> %ld", start_lsn, sec_count, img_offset); */
|
||||
}
|
||||
|
||||
|
||||
@@ -213,7 +212,7 @@ _cdio_parse_nero_footer (_img_private_t *_obj)
|
||||
if (_obj->size)
|
||||
return 0;
|
||||
|
||||
size = cdio_stream_stat (_obj->nrg_src);
|
||||
size = cdio_stream_stat (_obj->data_source);
|
||||
|
||||
{
|
||||
PRAGMA_BEGIN_PACKED
|
||||
@@ -231,9 +230,9 @@ PRAGMA_BEGIN_PACKED
|
||||
PRAGMA_END_PACKED
|
||||
|
||||
cdio_assert (sizeof (buf) == 12);
|
||||
|
||||
cdio_stream_seek (_obj->nrg_src, size - sizeof (buf));
|
||||
cdio_stream_read (_obj->nrg_src, (void *) &buf, sizeof (buf), 1);
|
||||
|
||||
cdio_stream_seek (_obj->data_source, size - sizeof (buf), SEEK_SET);
|
||||
cdio_stream_read (_obj->data_source, (void *) &buf, sizeof (buf), 1);
|
||||
|
||||
if (buf.v50.ID == UINT32_TO_BE (0x4e45524f)) /* "NERO" */
|
||||
{
|
||||
@@ -258,8 +257,8 @@ PRAGMA_END_PACKED
|
||||
|
||||
footer_buf = _cdio_malloc (size - footer_start);
|
||||
|
||||
cdio_stream_seek (_obj->nrg_src, footer_start);
|
||||
cdio_stream_read (_obj->nrg_src, footer_buf, size - footer_start, 1);
|
||||
cdio_stream_seek (_obj->data_source, footer_start, SEEK_SET);
|
||||
cdio_stream_read (_obj->data_source, footer_buf, size - footer_start, 1);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -487,7 +486,7 @@ _cdio_init (_img_private_t *_obj)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(_obj->nrg_src = cdio_stdio_new (_obj->source_name))) {
|
||||
if (!(_obj->data_source = cdio_stdio_new (_obj->source_name))) {
|
||||
cdio_error ("init failed");
|
||||
return false;
|
||||
}
|
||||
@@ -498,6 +497,76 @@ _cdio_init (_img_private_t *_obj)
|
||||
|
||||
}
|
||||
|
||||
/*!
|
||||
Reads into buf the next size bytes.
|
||||
Returns -1 on error.
|
||||
Would be libc's seek() but we have to adjust for the extra track header
|
||||
information in each sector.
|
||||
*/
|
||||
static off_t
|
||||
_cdio_lseek (void *user_data, off_t offset, int whence)
|
||||
{
|
||||
_img_private_t *_obj = user_data;
|
||||
|
||||
/* real_offset is the real byte offset inside the disk image
|
||||
The number below was determined empirically. I'm guessing
|
||||
the 1st 24 bytes of a bin file are used for something.
|
||||
*/
|
||||
off_t real_offset=8;
|
||||
|
||||
unsigned int i;
|
||||
unsigned int user_datasize;
|
||||
|
||||
for (i=0; i<_obj->total_tracks; i++) {
|
||||
track_info_t *this_track=&(_obj->tocent[i]);
|
||||
switch (this_track->track_format) {
|
||||
case TRACK_FORMAT_AUDIO:
|
||||
user_datasize=CDDA_SECTOR_SIZE;
|
||||
break;
|
||||
case TRACK_FORMAT_CDI:
|
||||
user_datasize=FORM1_DATA_SIZE;
|
||||
break;
|
||||
case TRACK_FORMAT_XA:
|
||||
user_datasize=FORM1_DATA_SIZE;
|
||||
break;
|
||||
default:
|
||||
cdio_warn ("track %d has unknown format %d",
|
||||
i+1, this_track->track_format);
|
||||
}
|
||||
|
||||
if ( (this_track->sec_count*user_datasize) >= offset) {
|
||||
int blocks = offset / user_datasize;
|
||||
int rem = offset % user_datasize;
|
||||
int block_offset = blocks * (M2RAW_SECTOR_SIZE);
|
||||
real_offset += block_offset + rem;
|
||||
break;
|
||||
}
|
||||
real_offset += this_track->sec_count*(M2RAW_SECTOR_SIZE);
|
||||
|
||||
offset -= this_track->sec_count*user_datasize;
|
||||
}
|
||||
|
||||
if (i==_obj->total_tracks) {
|
||||
cdio_warn ("seeking outside range of disk image");
|
||||
return -1;
|
||||
} else
|
||||
return cdio_stream_seek(_obj->data_source, real_offset, whence);
|
||||
}
|
||||
|
||||
/*!
|
||||
Reads into buf the next size bytes.
|
||||
Returns -1 on error.
|
||||
FIXME:
|
||||
At present we assume a read doesn't cross sector or track
|
||||
boundaries.
|
||||
*/
|
||||
static ssize_t
|
||||
_cdio_read (void *user_data, void *buf, size_t size)
|
||||
{
|
||||
_img_private_t *_obj = user_data;
|
||||
return cdio_stream_read(_obj->data_source, buf, size, 1);
|
||||
}
|
||||
|
||||
static uint32_t
|
||||
_cdio_stat_size (void *user_data)
|
||||
{
|
||||
@@ -524,7 +593,7 @@ _read_mode2_sector (void *user_data, void *data, lsn_t lsn, bool form2)
|
||||
{
|
||||
_mapping_t *_map = _cdio_list_node_data (node);
|
||||
|
||||
if (IN (lsn, _map->start_lsn, (_map->start_lsn + _map->secsize - 1)))
|
||||
if (IN (lsn, _map->start_lsn, (_map->start_lsn + _map->sec_count - 1)))
|
||||
{
|
||||
long img_offset = _map->img_offset;
|
||||
int blocksize = _obj->sector_2336_flag
|
||||
@@ -532,8 +601,8 @@ _read_mode2_sector (void *user_data, void *data, lsn_t lsn, bool form2)
|
||||
|
||||
img_offset += (lsn - _map->start_lsn) * blocksize;
|
||||
|
||||
cdio_stream_seek (_obj->nrg_src, img_offset);
|
||||
cdio_stream_read (_obj->nrg_src,
|
||||
cdio_stream_seek (_obj->data_source, img_offset, SEEK_SET);
|
||||
cdio_stream_read (_obj->data_source,
|
||||
_obj->sector_2336_flag ? (buf + 12 + 4) : buf,
|
||||
blocksize, 1);
|
||||
|
||||
@@ -547,7 +616,7 @@ _read_mode2_sector (void *user_data, void *data, lsn_t lsn, bool form2)
|
||||
if (form2)
|
||||
memcpy (data, buf + 12 + 4, M2RAW_SECTOR_SIZE);
|
||||
else
|
||||
memcpy (data, buf + 12 + 4 + 8, M2F1_SECTOR_SIZE);
|
||||
memcpy (data, buf + 12 + 4 + 8, FORM1_DATA_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -576,8 +645,8 @@ _read_mode2_sectors (void *user_data, void *data, uint32_t lsn,
|
||||
if ( (retval = _read_mode2_sector (_obj, buf, lsn + i, true)) )
|
||||
return retval;
|
||||
|
||||
memcpy (((char *)data) + (M2F1_SECTOR_SIZE * i), buf + 8,
|
||||
M2F1_SECTOR_SIZE);
|
||||
memcpy (((char *)data) + (FORM1_DATA_SIZE * i), buf + 8,
|
||||
FORM1_DATA_SIZE);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -700,6 +769,8 @@ _cdio_get_track_green(void *user_data, track_t track_num)
|
||||
/*!
|
||||
Return the starting MSF (minutes/secs/frames) for the track number
|
||||
track_num in obj. Tracks numbers start at 1.
|
||||
The "leadout" track is specified either by
|
||||
using track_num LEADOUT_TRACK or the total tracks+1.
|
||||
*/
|
||||
static bool
|
||||
_cdio_get_track_msf(void *user_data, track_t track_num, msf_t *msf)
|
||||
@@ -732,8 +803,10 @@ cdio_open_nrg (const char *source_name)
|
||||
.get_num_tracks = _cdio_get_num_tracks,
|
||||
.get_track_format = _cdio_get_track_format,
|
||||
.get_track_green = _cdio_get_track_green,
|
||||
.get_track_lba = NULL, /* This could be implemented if need be. */
|
||||
.get_track_lba = NULL, /* Will use generic routine via msf */
|
||||
.get_track_msf = _cdio_get_track_msf,
|
||||
.lseek = _cdio_lseek,
|
||||
.read = _cdio_read,
|
||||
.read_mode2_sector = _read_mode2_sector,
|
||||
.read_mode2_sectors = _read_mode2_sectors,
|
||||
.set_arg = _cdio_set_arg,
|
||||
|
||||
Reference in New Issue
Block a user