diff options
Diffstat (limited to 'lib/cdutils.cpp')
-rw-r--r-- | lib/cdutils.cpp | 157 |
1 files changed, 107 insertions, 50 deletions
diff --git a/lib/cdutils.cpp b/lib/cdutils.cpp index ea4ff4c..f39114c 100644 --- a/lib/cdutils.cpp +++ b/lib/cdutils.cpp @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: cdutils.cpp,v 1.33 2004-11-27 21:47:56 pixel Exp $ */ +/* $Id: cdutils.cpp,v 1.34 2005-11-02 21:34:02 pixel Exp $ */ #include <stdio.h> #include <string.h> @@ -26,6 +26,14 @@ #include "cdutils.h" #include "Output.h" +#ifdef WORDS_BIGENDIAN +#define IS_LITTLE_ENDIAN 0 +#define IS_BIG_ENDIAN 1 +#else +#define IS_LITTLE_ENDIAN 1 +#define IS_BIG_ENDIAN 0 +#endif + const long sec_sizes[7] = {0, 2048, 2336, 2048, 2324, 2352, 2352}; const long sec_offsts[7] = {0, 16, 16, 24, 24, 0, 0}; const String sec_modes[7] = {"MODE 0 (empty)", "MODE 1", "MODE 2", "MODE 2 FORM 1", "MODE 2 FORM 2", "Raw", "Autodetect"}; @@ -38,24 +46,24 @@ cdutils::~cdutils() { free(rootDir); } -unsigned char cdutils::from_BCD(unsigned char x) { +Uint8 cdutils::from_BCD(Uint8 x) { return ((x & 0xf) + ((x & 0xf0) >> 4) * 10); } -unsigned char cdutils::to_BCD(unsigned char x) { +Uint8 cdutils::to_BCD(Uint8 x) { return ((x / 10) << 4) | (x % 10); } -bool cdutils::is_valid_BCD(unsigned char x) { +bool cdutils::is_valid_BCD(Uint8 x) { return (((x & 15) < 10) && ((x >> 4) < 10)); } -unsigned long cdutils::from_MSF(unsigned char m, unsigned char s, unsigned char f, unsigned long start) { +Uint32 cdutils::from_MSF(Uint8 m, Uint8 s, Uint8 f, Uint32 start) { return (from_BCD(m) * 60 + from_BCD(s)) * 75 + from_BCD(f) - start; } -unsigned long cdutils::from_MSF(unsigned long msf, unsigned long start) { - unsigned char +Uint32 cdutils::from_MSF(Uint32 msf, Uint32 start) { + Uint8 f = msf & 0xff, s = (msf >> 8) & 0xff, m = (msf >> 16) & 0xff; @@ -63,7 +71,7 @@ unsigned long cdutils::from_MSF(unsigned long msf, unsigned long start) { return from_MSF(m, s, f, start); } -void cdutils::to_MSF(int sect, unsigned char & m, unsigned char & s, unsigned char & f, unsigned long start) { +void cdutils::to_MSF(int sect, Uint8 & m, Uint8 & s, Uint8 & f, Uint32 start) { sect += start; f = to_BCD(sect % 75); sect /= 75; @@ -71,8 +79,8 @@ void cdutils::to_MSF(int sect, unsigned char & m, unsigned char & s, unsigned ch m = to_BCD(sect / 60); } -unsigned long cdutils::to_MSF(int sect, unsigned long start) { - unsigned char m, s, f; +Uint32 cdutils::to_MSF(int sect, Uint32 start) { + Uint8 m, s, f; to_MSF(sect, m, s, f, start); return f | (s << 8) | (m << 16); } @@ -165,6 +173,38 @@ Uint32 cdutils::swap_dword(Uint32 i) { return (i >> 24) | ((i >> 8) & 0x0000ff00) | ((i << 8) & 0x00ff0000) | (i << 24); } +Uint16 cdutils::from_LE16(Uint16 i) { +#ifdef WORDS_BIGENDIAN + return swap_word(i); +#else + return i; +#endif +} + +Uint32 cdutils::from_LE32(Uint32 i) { +#ifdef WORDS_BIGENDIAN + return swap_dword(i); +#else + return i; +#endif +} + +Uint16 cdutils::from_BE16(Uint16 i) { +#ifdef WORDS_BIGENDIAN + return i; +#else + return swap_word(i); +#endif +} + +Uint32 cdutils::from_BE32(Uint32 i) { +#ifdef WORDS_BIGENDIAN + return i; +#else + return swap_dword(i); +#endif +} + int cdutils::guess_type(int number) { Byte header[24]; @@ -177,7 +217,7 @@ int cdutils::guess_type(int number) { if (header[15] == 1) { return MODE_1; } else if (header[15] == 2) { - if (*((unsigned long *) &(header[16])) == *((unsigned long *) &(header[20]))) { + if (*((Uint32 *) &(header[16])) == *((Uint32 *) &(header[20]))) { if ((header[16] == 0) && (header[17] == 0) && (header[19] == 0)) { if (header[18] & 0x20) { return MODE_2_FORM_2; @@ -224,7 +264,7 @@ long cdutils::read_sector(Byte * buffer, int type, int number) { return sec_sizes[type]; } -void cdutils::read_datas(Byte * buffer, long size, int type, int number) { +void cdutils::read_data(Byte * buffer, long size, int type, int number) { Byte sector[2352]; int i, n, reste; @@ -312,9 +352,9 @@ void cdutils::write_sector(Byte * buffer, int type, int number) throw (GeneralEx } } -void cdutils::write_datas(Byte * buffer, long size, int type, int number) { +void cdutils::write_data(Byte * buffer, long size, int type, int number) { long nbsectors, i; - unsigned char sector[2352]; + Uint8 sector[2352]; if (type == GUESS) { type = guess_type(number); @@ -339,7 +379,7 @@ void cdutils::write_datas(Byte * buffer, long size, int type, int number) { void cdutils::write_file(Handle * file, long size, int type, int number) { long nbsectors, i; - unsigned char buffer[2352]; + Uint8 buffer[2352]; if (type == GUESS) { type = guess_type(number); @@ -394,10 +434,15 @@ void cdutils::show_head_entry(void) { int cdutils::show_entry(struct DirEntry * dir) { char pbuf[200], pad; int s; + Uint32 Sector, Size; + if ((!dir) || (!dir->R)) { return 1; } + Sector = from_LE32(dir->Sector); + Size = from_LE32(dir->Size); + strncpy(pbuf, dir->id, dir->N); pbuf[dir->N] = 0; if ((dir->N == 1) && (pbuf[0] == 0)) { @@ -433,7 +478,7 @@ int cdutils::show_entry(struct DirEntry * dir) { dir->Year += 100; printm(M_BARE, "%6i %9i %2i/%02i/%04i %2i:%02i:%02i%+03.1f %c%c%c%c%c%c%c%c %s\n", - dir->Sector, dir->Size, dir->Day, dir->Month, dir->Year + 1900, dir->Hour, dir->Minute, dir->Second, ((float) dir->Offset) / 4, + Sector, Size, dir->Day, dir->Month, dir->Year + 1900, dir->Hour, dir->Minute, dir->Second, ((float) dir->Offset) / 4, dir->Flags & 1 ? 'H' : '-', dir->Flags & 2 ? 'D' : '-', dir->Flags & 4 ? 'A' : '-', dir->Flags & 8 ? 'R' : '-', dir->Flags & 16 ? 'P' : '-', dir->Flags & 32 ? '1' : '-', dir->Flags & 64 ? '1' : '-', dir->Flags & 128 ? 'C' : '-', pbuf); return dir->R; @@ -442,16 +487,20 @@ int cdutils::show_entry(struct DirEntry * dir) { int cdutils::show_dir(struct DirEntry * dir) { unsigned int ptr; Byte * buffer; + Uint32 Size, Sector; if (!(dir->Flags & 2)) { return 0; } - buffer = (Byte *) malloc(dir->Size); - read_datas(buffer, dir->Size, GUESS, dir->Sector); + Size = from_LE32(dir->Size); + Sector = from_LE32(dir->Sector); + + buffer = (Byte *) malloc(Size); + read_data(buffer, Size, GUESS, Sector); ptr = 0; - while(ptr < dir->Size) { + while(ptr < Size) { ptr += show_entry((struct DirEntry *) &(buffer[ptr])); } @@ -461,15 +510,19 @@ int cdutils::show_dir(struct DirEntry * dir) { struct cdutils::DirEntry cdutils::find_dir_entry(struct DirEntry * dir, String name) { unsigned int ptr, size; - unsigned char * buffer; + Uint8 * buffer; struct DirEntry r = {0, 0, 0, 0, 0}; + Uint32 Sector; if (!(dir->Flags & 2)) { return r; } - buffer = (unsigned char *) malloc(size = dir->Size); - read_datas(buffer, dir->Size, GUESS, dir->Sector); + Sector = from_LE32(dir->Sector); + size = from_LE32(dir->Size); + + buffer = (Uint8 *) malloc(size); + read_data(buffer, size, GUESS, Sector); ptr = 0; while(ptr < size) { @@ -493,13 +546,17 @@ struct cdutils::DirEntry * cdutils::find_dir_entry(Byte ** bufout, struct cdutil Byte * buffer; struct DirEntry * rdir = 0; *bufout = 0; + Uint32 Sector; if (!(dir->Flags & 2)) { return 0; } - buffer = (Byte *) malloc(size = dir->Size); - read_datas(buffer, dir->Size, GUESS, dir->Sector); + size = from_LE32(dir->Size); + Sector = from_LE32(dir->Sector); + + buffer = (Byte *) malloc(size); + read_data(buffer, size, GUESS, Sector); ptr = 0; while(ptr < size) { @@ -527,7 +584,7 @@ struct cdutils::DirEntry * cdutils::find_dir_entry(Byte ** bufout, struct cdutil int cdutils::show_iso_infos() { char buffer[2048]; char pbuff[130]; - short int s, nogood = 0; + int16 s, nogood = 0; read_sector((Byte *) buffer, GUESS, 16); @@ -537,7 +594,7 @@ int cdutils::show_iso_infos() { memcpy(pbuff, buffer + 1, 5); pbuff[5] = 0; printm(M_BARE, " 1 - 5-`CD001' : %s\n", pbuff); - printm(M_BARE, " 6 - 2- 1 : %i\n", s = *((short int *) &(buffer[6]))); + printm(M_BARE, " 6 - 2- 1 : %i\n", s = from_LE16(*((Uint16 *) &(buffer[6])))); printm(M_BARE, "(*this was the signature*)\n"); if (buffer[0] != 1) nogood = 1; if (strcmp(pbuff, "CD001")) nogood = 1; @@ -554,15 +611,15 @@ int cdutils::show_iso_infos() { memcpy(pbuff, buffer + 40, 32); pbuff[32] = 0; printm(M_BARE, " 40 - 32- VOLID : %s\n", pbuff); - printm(M_BARE, " 80 - 8- SNum : %li\n", *((long int *) &(buffer[80]))); - printm(M_BARE, " 120 - 4- VOLSiz : %i\n", *((short int *) &(buffer[120]))); - printm(M_BARE, " 124 - 4- VOLNum : %i\n", *((short int *) &(buffer[124]))); - printm(M_BARE, " 128 - 4- SSize : %i\n", *((short int *) &(buffer[128]))); - printm(M_BARE, " 132 - 8- PSize : %li\n", *((long int *) &(buffer[132]))); - printm(M_BARE, " 140 - 4- 1SLPath: %i\n", *((long int *) &(buffer[140]))); - printm(M_BARE, " 144 - 4- 2SLPath: %i\n", *((long int *) &(buffer[144]))); - printm(M_BARE, " 148 - 4- 1SBPath: %i\n", swap_word(*((long int *) &(buffer[150])))); - printm(M_BARE, " 152 - 4- 2SBPath: %i\n", swap_word(*((long int *) &(buffer[154])))); + printm(M_BARE, " 80 - 8- SNum : %li\n", from_LE32(*((Uint32 *) &(buffer[80])))); + printm(M_BARE, " 120 - 4- VOLSiz : %i\n", from_LE16(*((Uint16 *) &(buffer[120])))); + printm(M_BARE, " 124 - 4- VOLNum : %i\n", from_LE16(*((Uint16 *) &(buffer[124])))); + printm(M_BARE, " 128 - 4- SSize : %i\n", from_LE16(*((Uint16 *) &(buffer[128])))); + printm(M_BARE, " 132 - 8- PSize : %li\n", from_LE32(*((Uint32 *) &(buffer[132])))); + printm(M_BARE, " 140 - 4- 1SLPath: %li\n", from_LE32(*((Uint32 *) &(buffer[140])))); + printm(M_BARE, " 144 - 4- 2SLPath: %li\n", from_LE32(*((Uint32 *) &(buffer[144])))); + printm(M_BARE, " 148 - 4- 1SBPath: %li\n", from_BE32(*((Uint32 *) &(buffer[148])))); + printm(M_BARE, " 152 - 4- 2SBPath: %li\n", from_BE32(*((Uint32 *) &(buffer[152])))); memcpy(pbuff, buffer + 190, 128); pbuff[128] = 0; printm(M_BARE, " 190 - 128- VStId : %s\n", pbuff); @@ -599,7 +656,7 @@ int cdutils::show_iso_infos() { int cdutils::get_iso_infos() { Byte buffer[2048]; char pbuff[130]; - short int s, nogood = 0; + int16 s, nogood = 0; int rootsec; read_sector(buffer, GUESS, 16); @@ -607,7 +664,7 @@ int cdutils::get_iso_infos() { memcpy(pbuff, buffer + 1, 5); pbuff[5] = 0; - s = *((short int *) &(buffer[6])); + s = from_LE16(*((Uint16 *) &(buffer[6]))); if (buffer[0] != 1) nogood = 1; if (strcmp(pbuff, "CD001")) nogood = 1; if (s != 1) nogood = 1; @@ -617,11 +674,11 @@ int cdutils::get_iso_infos() { return 0; } - pt1 = *((short int *) &(buffer[140])); - pt2 = *((short int *) &(buffer[144])); - snum = *((int *) &(buffer[80])); - ptl = *((long int *) &(buffer[132])); - rootsec = ((struct DirEntry *) (&buffer[156]))->Sector; + pt1 = from_LE16(*((Uint16 *) &(buffer[140]))); + pt2 = from_LE16(*((Uint16 *) &(buffer[144]))); + snum = from_LE32(*((Uint32 *) &(buffer[80]))); + ptl = from_LE32(*((Uint32 *) &(buffer[132]))); + rootsec = from_LE32(((struct DirEntry *) (&buffer[156]))->Sector); read_sector(buffer, GUESS, rootsec); rootDir = (struct DirEntry *) malloc(buffer[0]); memcpy(rootDir, buffer, buffer[0]); @@ -641,7 +698,7 @@ int cdutils::get_pt_infos() { } buffer = (Byte *) malloc(ptl); - read_datas(buffer, ptl, GUESS, !pt1 ? pt2 : pt1); + read_data(buffer, ptl, GUESS, !pt1 ? pt2 : pt1); if (buffer[0] == 1) if (buffer[1] == 0) @@ -649,7 +706,7 @@ int cdutils::get_pt_infos() { if (buffer[7] == 0) if (buffer[8] == 0) if (buffer[9] == 0) - root = *((unsigned long int *) &(buffer[2])); + root = from_LE32(*((Uint32 *) &(buffer[2]))); free(buffer); return root ? 1 : 0; @@ -670,13 +727,13 @@ int cdutils::show_pt_infos() { } buffer = (Byte *) malloc(ptl + 2); - read_datas(buffer, ptl, GUESS, !pt1 ? pt2 : pt1); + read_data(buffer, ptl, GUESS, !pt1 ? pt2 : pt1); printm(M_BARE, "node^paren@sector : name\n"); for (ptr = 0, i = 1; buffer[ptr]; ptr += ptr & 1, i++) { strncpy(pbuf, (char *) &(buffer[8 + ptr]), buffer[ptr]); pbuf[buffer[ptr]] = 0; - printm(M_BARE, "%3i ^ %3i @ %6i: %s\n", i, *((unsigned short *) &(buffer[6 + ptr])), *((unsigned long *) &(buffer[2 + ptr])), pbuf); + printm(M_BARE, "%3i ^ %3i @ %6i: %s\n", i, from_LE16(*((Uint16 *) &(buffer[6 + ptr]))), from_LE32(*((Uint32 *) &(buffer[2 + ptr]))), pbuf); ptr += 8 + buffer[ptr]; } @@ -862,7 +919,7 @@ cdfile::cdfile(cdutils * _cd, const cdutils::DirEntry * d, int _mode) : Handle(- } cdfile::cdfile(cdutils * _cd, int _sector, ssize_t _size, int _mode) : Handle(-1), cd(_cd), sector(_sector), mode(_mode), size(_size), name("raw reading") { - Byte datas[2352]; + Byte data[2352]; bool eof; if (mode == GUESS) { @@ -872,8 +929,8 @@ cdfile::cdfile(cdutils * _cd, int _sector, ssize_t _size, int _mode) : Handle(-1 size = 0; if ((mode == MODE2_FORM1) || (mode == MODE2_FORM2)) { do { - cd->read_sector(datas, MODE_RAW, sector + size++); - eof = datas[18] & 0x80; + cd->read_sector(data, MODE_RAW, sector + size++); + eof = data[18] & 0x80; } while (!eof); size *= sec_sizes[mode]; } @@ -908,7 +965,7 @@ ssize_t cdfile::read(void *buf, size_t count) throw (GeneralException) { buf = (Byte *) buf + nstartbytes; if (count) { - cd->read_datas((Byte *) buf, count, mode, startsec + 1); + cd->read_data((Byte *) buf, count, mode, startsec + 1); } itell += count + nstartbytes; |