there is 2 forms in mode2, from1 where there is 2048 bytes of data + some bytes of error correction, and form2 where the error correction isn't there, and it's just data.
nicer explanationobviously, if you want to be able to read that data, you must be able to read the sectors in raw format, and then check the sector mode to know if it's using form1 or form2.
I don't get why do you say the file is never 2k. The file canbe anything from 1 byte to the whole CD... you only need to read the sector header to be able to tell if that sector contains 2048 or 2324 bytes, after that you can just return those bytes without ever telling the "caller" how were they stored in the disk...
to read any number of bytes from any position
you just need a "sector buffer"
2352 bytes in size
then
have some "current pointer" and "bytes left in sector" values, then just fill the buffer till you read all the data, or reached the end of the file. like:
//fast, lame and poorly named code
int read_data(char* buffer_pointer, int bytes_to_read)
{
int bytes_read=0;
while((bytes_to_read>0)&&(bytes_left_in_file>0))
{
if(bytes_in_sector==0) read_next_sector(§or_pointer,&bytes_in_sector);
int bytes_to_copy=min(bytes_in_sector,bytes_to_read);
memcpy(buffer_pointer,sector_pointer,bytes_to_copy);
buffer_pointer+=bytes_to_copy;
sector_pointer+=bytes_to_copy;
bytes_left_in_file-=bytes_to_copy;
bytes_to_read-=bytes_to_copy;
bytes_read+=bytes_to_copy;
bytes_in_sector-=bytes_to_copy;
}
return bytes_read;
}
This code assumes it's contained in some class and all the vars not explicitly declared are assumed to be instance variables initialized before calling.
(someone might remember I said I hate _ in code, and I feel it's really stupid that I preferred them in this case... O_o).