From 08ecf5aae1c7276bb1e78a288cba3a731604758e Mon Sep 17 00:00:00 2001 From: Pixel Date: Fri, 16 Aug 2002 15:03:06 +0000 Subject: Everything's broken :-P --- lib/cdutils.cpp | 283 +++++++++++--------------------------------------------- 1 file changed, 54 insertions(+), 229 deletions(-) (limited to 'lib') diff --git a/lib/cdutils.cpp b/lib/cdutils.cpp index 0dd0e8a..57c1068 100644 --- a/lib/cdutils.cpp +++ b/lib/cdutils.cpp @@ -24,9 +24,9 @@ #include #include "generic.h" #include "cdutils.h" -#include "fileutils.h" +#include "Input.h" -FILE * ppf_file = 0; +Output * ppf_file = 0; int pt1 = -1, pt2 = -1, snum = 0, ptl = 0, root = 0; long sec_sizes[5] = {0, 2048, 2336, 2048, 2324}; @@ -60,148 +60,89 @@ unsigned long from_MSF(unsigned long msf, unsigned long start) { return from_MSF(m, s, f, start); } -FILE * open_ppf(char * ppf, FILE * iso, char * comment) { +Input * open_ppf(String ppf, Handle * iso, String comment) { int i, l; - char buffer[1024]; + Byte buffer[1024]; - if (!(ppf_file = fopen(ppf, "w"))) - return ppf_file; - fwrite("PPF20\001", 1, 6, ppf_file); + ppf_file = new Output(ppf); + ppf_file->write("PPF20\001", 6); - l = strlen(comment); + l = comment.strlen(); if (l >= 50) { - fwrite(comment, 1, 50, ppf_file); + ppf_file->write(comment.to_char(), 50); } else { char * t = " "; - fwrite(comment, 1, l, ppf_file); + ppf_file->write(comment, l); for (i = l; i < 50; i++) { - fwrite(t, 1, 1, ppf_file); + ppf_file->write(t, 1); } } - l = filesize(iso); - fwrite(&l, 1, 4, ppf_file); + l = iso->GetSize(); + ppf_file->write(&l, sizeof(l)); - fseek(iso, 0x9320, SEEK_SET); - fread(buffer, 1, 1024, iso); - fwrite(buffer, 1, 1024, ppf_file); + iso->seek(0x9320, SEEK_SET); + iso->read(buffer, 1024); + ppf_file->write(buffer, 1024); return ppf_file; } -FILE * open_ppf(char * ppf, int iso, char * comment) { - int i, l; - char buffer[1024]; - - if (!(ppf_file = fopen(ppf, "w"))) - return ppf_file; - fwrite("PPF20\001", 1, 6, ppf_file); - - l = strlen(comment); - if (l >= 50) { - fwrite(comment, 1, 50, ppf_file); - } else { - char * t = " "; - fwrite(comment, 1, l, ppf_file); - for (i = l; i < 50; i++) { - fwrite(t, 1, 1, ppf_file); - } - } - - l = filesize(iso); - fwrite(&l, 1, 4, ppf_file); - - lseek(iso, 0x9320, SEEK_SET); - read(iso, buffer, 1024); - fwrite(buffer, 1, 1024, ppf_file); - return ppf_file; -} - -void write_ppf(unsigned char * old_sec, unsigned char * new_sec, int sec_num) { +void write_ppf(Byte * old_sec, Byte * new_sec, int sec_num) { int i, l = 0, o; for (i = 0; i < 2352; i++) { if (old_sec[i] == new_sec[i]) { if (l != 0) { o = 2352 * sec_num + i; - fwrite(&o, 1, 4, ppf_file); - fwrite(&l, 1, 1, ppf_file); - fwrite(new_sec + i - l - 1, 1, l, ppf_file); + ppf_file->write(&o, sizeof(o)); + ppf_file->write(&l, 1); + ppf_file->write(new_sec + i - l - 1, l); l = 0; } } else { l++; if (l == 255) { o = 2352 * sec_num + i; - fwrite(&o, 1, 4, ppf_file); - fwrite(&l, 1, 1, ppf_file); - fwrite(new_sec + i - 255, 1, 255, ppf_file); + ppf_file->write(&o, 4); + ppf_file->write(&l, 1); + ppf_file->write(new_sec + i - 255, 255); l = 0; } } } } -char * format_date(unsigned char * input) { - static char output[26]; - - memcpy(output, input + 6, 2); - output[2] = '/'; - memcpy(output + 3, input + 4, 2); - output[5] = '/'; - memcpy(output + 6, input, 4); - output[10] = ' '; - memcpy(output + 11, input + 8, 2); - output[13] = ':'; - memcpy(output + 14, input + 10, 2); - output[16] = ':'; - memcpy(output + 17, input + 12, 2); - output[19] = '.'; - memcpy(output + 20, input + 14, 2); - sprintf(output + 22, "%+3.1f", ((float) (*(((char *)input) + 16))) / 4); - +String format_date(String input) { + String output; + + output = input.extract(6, 7) + '/'; + output += input.extract(4, 5) + '/'; + output += input.extract(0, 3) + ' '; + output += input.extract(8, 9) + ':'; + output += input.extract(10, 11) + ':'; + output += input.extract(12, 13) + '.'; + output += input.extract(14, 15); + output += String("").set("%+3.1f", ((float) (*(((char *)input) + 16))) / 4); return output; } -unsigned short int swap_word(unsigned short int i) { +Uint16 swap_word(Uint16 i) { return (i >> 8) | (i << 8); } -unsigned long int swap_dword(unsigned long int i) { +Uint32 swap_dword(Uint32 i) { return (i >> 24) | ((i >> 8) & 0x0000ff00) | ((i << 8) & 0x00ff0000) | (i << 24); } -void sector_seek(FILE * f_iso, long sector) { - long curpos; - - curpos = (2352 * (long) sector); - if (fseek(f_iso, curpos, SEEK_SET)) { - printm(M_ERROR, "Unable to seek at %i\n", curpos); - exit(-1); - } else if (ftell(f_iso) != curpos) { - printm(M_ERROR, "Seek slipped when seeking at %i\n", curpos); - exit(-1); - } -} - -void sector_seek(int f_iso, long sector) { - long curpos; - - curpos = (2352 * (long) sector); - if (lseek(f_iso, curpos, SEEK_SET) != curpos) { - printm(M_ERROR, "Unable to seek at %i\n", curpos); - exit(-1); - } -} - -int guess_type(FILE * f_iso, int number) { - unsigned char header[24]; +int guess_type(Handle * f_iso, int number) { + Byte header[24]; if (number >= 0) { sector_seek(f_iso, number); } - fread(header, 1, 24, f_iso); - fseek(f_iso, -24, SEEK_CUR); + f_iso->read(header, 24); + f_iso->seek(-24, SEEK_CUR); if (header[15] == 1) { return MODE_1; } else if (header[15] == 2) { @@ -220,34 +161,7 @@ int guess_type(FILE * f_iso, int number) { return MODE_0; } -int guess_type(int f_iso, int number) { - unsigned char header[24]; - - if (number >= 0) { - sector_seek(f_iso, number); - } - - read(f_iso, header, 24); - lseek(f_iso, -24, SEEK_CUR); - if (header[15] == 1) { - return MODE_1; - } else if (header[15] == 2) { - if (*((unsigned long *) &(header[16])) == *((unsigned long *) &(header[20]))) { - if ((header[16] == 0) && (header[17] == 0) && (header[19] == 0)) { - if (header[18] & 0x20) { - return MODE_2_FORM_2; - } else { - return MODE_2_FORM_1; - } - } - } - return MODE_2; - } - - return MODE_0; -} - -long read_sector(FILE * f_iso, unsigned char * buffer, char type, int number) { +long read_sector(Handle * f_iso, Byte * buffer, int type, int number) { if (number >= 0) { sector_seek(f_iso, number); } @@ -256,51 +170,14 @@ long read_sector(FILE * f_iso, unsigned char * buffer, char type, int number) { type = guess_type(f_iso, number); } - fseek(f_iso, sec_offsts[type], SEEK_CUR); - fread(buffer, 1, sec_sizes[type], f_iso); - fseek(f_iso, 2352 - sec_offsts[type] - sec_sizes[type], SEEK_CUR); + f_iso->seek(sec_offsts[type], SEEK_CUR); + f_iso->read(buffer, sec_sizes[type], f_iso); + f_iso->seek(2352 - sec_offsts[type] - sec_sizes[type], SEEK_CUR); return sec_sizes[type]; } -long read_sector(int f_iso, unsigned char * buffer, char type, int number) { - if (number >= 0) { - sector_seek(f_iso, number); - } - - if (type == GUESS) { - type = guess_type(f_iso, number); - } - - lseek(f_iso, sec_offsts[type], SEEK_CUR); - read(f_iso, buffer, sec_sizes[type]); - lseek(f_iso, 2352 - sec_offsts[type] - sec_sizes[type], SEEK_CUR); - return sec_sizes[type]; -} - -void read_datas(FILE * f_iso, unsigned char * buffer, int type, int number, long size) { - unsigned char sector[2352]; - int i, n, reste; - - if (type == GUESS) { - type = guess_type(f_iso, number); - } - - n = size / sec_sizes[type]; - reste = size - n * sec_sizes[type]; - - sector_seek(f_iso, number); - for (i = 0; i < n; i++) { - read_sector(f_iso, buffer + i * sec_sizes[type], type); - } - - if (reste) { - read_sector(f_iso, sector, type); - bcopy((char *) sector, (char *) (buffer + n * sec_sizes[type]), reste); - } -} - -void read_datas(int f_iso, unsigned char * buffer, int type, int number, long size) { - unsigned char sector[2352]; +void read_datas(Handle * f_iso, Byte * buffer, int type, int number, long size) { + Byte sector[2352]; int i, n, reste; if (type == GUESS) { @@ -321,8 +198,8 @@ void read_datas(int f_iso, unsigned char * buffer, int type, int number, long si } } -void read_file(FILE * f_iso, FILE * file, char type, int number, long size) { - unsigned char sector[2352]; +void read_file(Handle * f_iso, Handle * file, int type, int number, long size) { + Byte sector[2352]; int i, n, reste; if (type == GUESS) { @@ -335,69 +212,17 @@ void read_file(FILE * f_iso, FILE * file, char type, int number, long size) { sector_seek(f_iso, number); for (i = 0; i < n; i++) { read_sector(f_iso, sector, type); - fwrite(sector, 1, sec_sizes[type], file); + file->write(sector, sec_sizes[type]); } if (reste) { read_sector(f_iso, sector, type); - fwrite(sector, 1, reste, file); - } -} - -void read_file(int f_iso, int file, char type, int number, long size) { - unsigned char sector[2352]; - int i, n, reste; - - if (type == GUESS) { - type = guess_type(f_iso, number); - } - - n = size / sec_sizes[type]; - reste = size - n * sec_sizes[type]; - - sector_seek(f_iso, number); - for (i = 0; i < n; i++) { - read_sector(f_iso, sector, type); - write(file, sector, sec_sizes[type]); - } - - if (reste) { - read_sector(f_iso, sector, type); - write(file, sector, reste); - } -} - -void write_sector(FILE * f_iso_r, FILE * f_iso_w, unsigned char * buffer, char type, int number) { - unsigned char old_sector[2352], new_sector[2352]; - - if (type == GUESS) { - type = guess_type(f_iso_r, number); - } - - if (number >= 0) { - sector_seek(f_iso_r, number); - sector_seek(f_iso_w, number); - } - - fread(old_sector, 1, 2352, f_iso_r); - - minute = old_sector[12]; - second = old_sector[13]; - frame = old_sector[14]; - - bcopy((char *) old_sector, (char *) new_sector, 2532); - bcopy((char *) buffer, (char *) new_sector + sec_offsts[type], sec_sizes[type]); - - do_encode_L2(new_sector, type, 0); - if (!ppf_file) { - fwrite(new_sector, 1, 2352, f_iso_w); - } else { - write_ppf(old_sector, new_sector, number); + file->write(sector, reste); } } -void write_sector(int f_iso_r, int f_iso_w, unsigned char * buffer, char type, int number) { - unsigned char old_sector[2352], new_sector[2352]; +void write_sector(Handle * f_iso_r, Handle * f_iso_w, Buffer * buffer, int type, int number) { + Byte old_sector[2352], new_sector[2352]; if (type == GUESS) { type = guess_type(f_iso_r, number); @@ -408,7 +233,7 @@ void write_sector(int f_iso_r, int f_iso_w, unsigned char * buffer, char type, i sector_seek(f_iso_w, number); } - read(f_iso_r, old_sector, 2352); + f_iso_r->read(old_sector, 2352); minute = old_sector[12]; second = old_sector[13]; @@ -419,13 +244,13 @@ void write_sector(int f_iso_r, int f_iso_w, unsigned char * buffer, char type, i do_encode_L2(new_sector, type, 0); if (!ppf_file) { - write(f_iso_w, new_sector, 2352); + f_iso_w->write(new_sector, 2352); } else { write_ppf(old_sector, new_sector, number); } } -void write_datas(FILE * f_iso_r, FILE * f_iso_w, unsigned char * buffer, char type, int number, long size) { +void write_datas(Handle * f_iso_r, Handle * f_iso_w, Byte * buffer, int type, int number, long size) { long nbsectors, i; unsigned char sector[2352]; -- cgit v1.2.3