From 492ca575941d0e30b3725bf7a9966cab8df7e238 Mon Sep 17 00:00:00 2001 From: Pixel Date: Fri, 19 Jul 2002 12:52:47 +0000 Subject: Moved everything in the right place. Cleaner now. --- lib/Makefile | 11 + lib/cdutils.cpp | 1127 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/crctable.out | 91 +++++ lib/crctables | 142 +++++++ lib/dteutils.cpp | 342 ++++++++++++++++ lib/fileutils.cpp | 74 ++++ lib/generic.cpp | 56 +++ lib/lzss.cpp | 438 +++++++++++++++++++++ lib/yazedc.cpp | 858 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 3139 insertions(+) create mode 100755 lib/Makefile create mode 100644 lib/cdutils.cpp create mode 100644 lib/crctable.out create mode 100644 lib/crctables create mode 100644 lib/dteutils.cpp create mode 100644 lib/fileutils.cpp create mode 100644 lib/generic.cpp create mode 100644 lib/lzss.cpp create mode 100644 lib/yazedc.cpp (limited to 'lib') diff --git a/lib/Makefile b/lib/Makefile new file mode 100755 index 0000000..5503701 --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,11 @@ +#!/usr/bin/make -f + +CPPFLAGS=-Wall -g -O3 -mcpu=i686 -pedantic -pedantic-errors -Werror -I../includes +CXX=g++ + +TARGET = cdutils.o dteutils.o fileutils.o generic.o lzss.o yazedc.o + +all: ${TARGET} + +clean: + rm -f *.o ${TARGET} compil.c diff --git a/lib/cdutils.cpp b/lib/cdutils.cpp new file mode 100644 index 0000000..0dd0e8a --- /dev/null +++ b/lib/cdutils.cpp @@ -0,0 +1,1127 @@ +/* + * PSX-Tools Bundle Pack + * Copyright (C) 2002 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include "generic.h" +#include "cdutils.h" +#include "fileutils.h" + +FILE * ppf_file = 0; +int pt1 = -1, pt2 = -1, snum = 0, ptl = 0, root = 0; + +long sec_sizes[5] = {0, 2048, 2336, 2048, 2324}; +long sec_offsts[5] = {0, 16, 16, 24, 24}; +char * sec_modes[5] = {"MODE 0 (no mode)", "MODE 1", "MODE 2", "MODE 2 FORM 1", "MODE 2 FORM 2"}; + +struct DirEntry rootDir; + +unsigned char from_BCD(unsigned char x) { + return ((x & 0xf) + ((x & 0xf0) >> 4) * 10); +} + +unsigned char to_BCD(unsigned char x) { + return ((x / 10) << 4) | (x % 10); +} + +int is_valid_BCD(unsigned char x) { + return (((x & 15) < 10) && ((x >> 4) < 10)); +} + +unsigned long from_MSF(unsigned char m, unsigned char s, unsigned char f, unsigned long start) { + return (from_BCD(m) * 60 + from_BCD(s)) * 75 + from_BCD(f) - start; +} + +unsigned long from_MSF(unsigned long msf, unsigned long start) { + unsigned char + f = msf & 0xff, + s = (msf >> 8) & 0xff, + m = (msf >> 16) & 0xff; + + return from_MSF(m, s, f, start); +} + +FILE * open_ppf(char * ppf, FILE * 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); + + fseek(iso, 0x9320, SEEK_SET); + fread(buffer, 1, 1024, iso); + fwrite(buffer, 1, 1024, ppf_file); + 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) { + 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); + 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); + 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); + + return output; +} + +unsigned short int swap_word(unsigned short int i) { + return (i >> 8) | (i << 8); +} + +unsigned long int swap_dword(unsigned long int 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]; + + if (number >= 0) { + sector_seek(f_iso, number); + } + + fread(header, 1, 24, f_iso); + fseek(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; +} + +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) { + if (number >= 0) { + sector_seek(f_iso, number); + } + + if (type == GUESS) { + 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); + 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]; + 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_file(FILE * f_iso, FILE * 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); + fwrite(sector, 1, sec_sizes[type], file); + } + + 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); + } +} + +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]; + + 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); + } + + read(f_iso_r, old_sector, 2352); + + 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) { + write(f_iso_w, 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) { + long nbsectors, i; + unsigned char 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); + } + + nbsectors = size / sec_sizes[type]; + + for (i = 0; i < nbsectors; i++) { + write_sector(f_iso_r, f_iso_w, buffer + i * sec_sizes[type], type, number + i); + } + + if (size % sec_sizes[type]) { + memset(sector, 0, 2352); + bcopy((char *) (buffer + i * sec_sizes[type]), (char *) sector, size % sec_sizes[type]); + write_sector(f_iso_r, f_iso_w, sector, type, number + i); + } +} + +void write_datas(int f_iso_r, int f_iso_w, unsigned char * buffer, char type, int number, long size) { + long nbsectors, i; + unsigned char 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); + } + + nbsectors = size / sec_sizes[type]; + + for (i = 0; i < nbsectors; i++) { + write_sector(f_iso_r, f_iso_w, buffer + i * sec_sizes[type], type, number + i); + } + + if (size % sec_sizes[type]) { + memset(sector, 0, 2352); + bcopy((char *) (buffer + i * sec_sizes[type]), (char *) sector, size % sec_sizes[type]); + write_sector(f_iso_r, f_iso_w, sector, type, number + i); + } +} + +void write_file(FILE * f_iso_r, FILE * f_iso_w, FILE * file, char type, int number) { + long size, nbsectors, i; + unsigned char buffer[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); + } + + size = filesize(file); + nbsectors = size / sec_sizes[type]; + + if (size % sec_sizes[type]) { + nbsectors++; + } + + for (i = 0; i < nbsectors; i++) { + memset(buffer, 0, 2352); + fread(buffer, 1, sec_sizes[type], file); + write_sector(f_iso_r, f_iso_w, buffer, type); + } +} + +void write_file(int f_iso_r, int f_iso_w, int file, char type, int number) { + long size, nbsectors, i; + unsigned char buffer[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); + } + + size = filesize(file); + nbsectors = size / sec_sizes[type]; + + if (size % sec_sizes[type]) { + nbsectors++; + } + + for (i = 0; i < nbsectors; i++) { + memset(buffer, 0, 2352); + read(file, buffer, sec_sizes[type]); + write_sector(f_iso_r, f_iso_w, buffer, type); + } +} + +void show_head_entry(void) { + printm(M_BARE, "Sector - Size - Date - Time - Flags - Name\n"); +} + +int show_entry(struct DirEntry * dir) { + char pbuf[200]; + if (!dir->R) { + return 1; + } + + strncpy(pbuf, (char *) &(dir->id), dir->N); + pbuf[dir->N] = 0; + + printm(M_BARE, "%6i - %8i - %2i/%02i/%02i - %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, 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; +} + +int show_dir(FILE * f_iso, struct DirEntry * dir) { + unsigned int ptr; + unsigned char * buffer; + + if (!(dir->Flags & 2)) { + return 0; + } + + buffer = (unsigned char *) malloc(dir->Size); + read_datas(f_iso, buffer, GUESS, dir->Sector, dir->Size); + + ptr = 0; + while(ptr < dir->Size) { + ptr += show_entry((struct DirEntry *) &(buffer[ptr])); + } + + free(buffer); + return 1; +} + +int show_dir(int f_iso, struct DirEntry * dir) { + unsigned int ptr; + unsigned char * buffer; + + if (!(dir->Flags & 2)) { + return 0; + } + + buffer = (unsigned char *) malloc(dir->Size); + read_datas(f_iso, buffer, GUESS, dir->Sector, dir->Size); + + ptr = 0; + while(ptr < dir->Size) { + ptr += show_entry((struct DirEntry *) &(buffer[ptr])); + } + + free(buffer); + return 1; +} + +struct DirEntry find_dir_entry(FILE * f_iso, struct DirEntry * dir, char * name) { + unsigned int ptr, size; + unsigned char * buffer; + struct DirEntry r = {0, 0, 0, 0, 0}; + + if (!(dir->Flags & 2)) { + return r; + } + + buffer = (unsigned char *) malloc(size = dir->Size); + read_datas(f_iso, buffer, GUESS, dir->Sector, dir->Size); + + ptr = 0; + while(ptr < size) { + dir = (struct DirEntry *) &(buffer[ptr]); + if (!dir->R) { + ptr++; + } else { + if (!strncmp(name, (char *) &(dir->id), dir->N)) { + r = *dir; + } + ptr += dir->R; + } + } + + free(buffer); + return r; +} + +struct DirEntry find_dir_entry(int f_iso, struct DirEntry * dir, char * name) { + unsigned int ptr, size; + unsigned char * buffer; + struct DirEntry r = {0, 0, 0, 0, 0}; + + if (!(dir->Flags & 2)) { + return r; + } + + buffer = (unsigned char *) malloc(size = dir->Size); + read_datas(f_iso, buffer, GUESS, dir->Sector, dir->Size); + + ptr = 0; + while(ptr < size) { + dir = (struct DirEntry *) &(buffer[ptr]); + if (!dir->R) { + ptr++; + } else { + if (!strncmp(name, (char *) &(dir->id), dir->N)) { + r = *dir; + } + ptr += dir->R; + } + } + + free(buffer); + return r; +} + +struct DirEntry * find_dir_entry(FILE * f_iso, unsigned char ** bufout, struct DirEntry * dir, char * name) { + unsigned int ptr, size; + unsigned char * buffer; + struct DirEntry * rdir = 0; + *bufout = 0; + + if (!(dir->Flags & 2)) { + return 0; + } + + buffer = (unsigned char *) malloc(size = dir->Size); + read_datas(f_iso, buffer, GUESS, dir->Sector, dir->Size); + + ptr = 0; + while(ptr < size) { + dir = (struct DirEntry *) &(buffer[ptr]); + if (!dir->R) { + ptr++; + } else { + if (!strncmp(name, (char *) &(dir->id), dir->N)) { + rdir = dir; + } + ptr += dir->R; + } + } + + if (rdir->R) { + *bufout = buffer; + } else { + free(buffer); + } + return rdir; +} + +struct DirEntry * find_dir_entry(int f_iso, unsigned char ** bufout, struct DirEntry * dir, char * name) { + unsigned int ptr, size; + unsigned char * buffer; + struct DirEntry * rdir = 0; + *bufout = 0; + + if (!(dir->Flags & 2)) { + return 0; + } + + buffer = (unsigned char *) malloc(size = dir->Size); + read_datas(f_iso, buffer, GUESS, dir->Sector, dir->Size); + + ptr = 0; + while(ptr < size) { + dir = (struct DirEntry *) &(buffer[ptr]); + if (!dir->R) { + ptr++; + } else { + if (!strncmp(name, (char *) &(dir->id), dir->N)) { + rdir = dir; + } + ptr += dir->R; + } + } + + if (rdir->R) { + *bufout = buffer; + } else { + free(buffer); + } + return rdir; +} + +int show_iso_infos(FILE * f_iso) { + unsigned char buffer[2048]; + char pbuff[130]; + short int s, nogood = 0; + + read_sector(f_iso, buffer, GUESS, 16); + + printm(M_BARE, "Sector guessed mode : %s\n", sec_modes[guess_type(f_iso, 16)]); + printm(M_BARE, "offset-size-info : contents\n"); + printm(M_BARE, " 0 - 1- 1 : %i\n", buffer[0]); + 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, "(*this was the signature*)\n"); + if (buffer[0] != 1) nogood = 1; + if (strcmp(pbuff, "CD001")) nogood = 1; + if (s != 1) nogood = 1; + + if (nogood) { + printm(M_BARE, "Not a valid iso9660 file.\n"); + return 0; + } + + memcpy(pbuff, buffer + 8, 32); + pbuff[32] = 0; + printm(M_BARE, " 8 - 32- SYSID : %s\n", pbuff); + 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", *((short int *) &(buffer[140]))); + printm(M_BARE, " 144 - 4- 2SLPath: %i\n", *((short int *) &(buffer[144]))); + printm(M_BARE, " 148 - 4- 1SBPath: %i\n", swap_dword(*((short int *) &(buffer[148])))); + printm(M_BARE, " 152 - 4- 2SBPath: %i\n", swap_dword(*((short int *) &(buffer[152])))); + memcpy(pbuff, buffer + 190, 128); + pbuff[128] = 0; + printm(M_BARE, " 190 - 128- VStId : %s\n", pbuff); + memcpy(pbuff, buffer + 318, 128); + pbuff[128] = 0; + printm(M_BARE, " 318 - 128- PubId : %s\n", pbuff); + memcpy(pbuff, buffer + 446, 128); + pbuff[128] = 0; + printm(M_BARE, " 446 - 128- DPrId : %s\n", pbuff); + memcpy(pbuff, buffer + 574, 128); + pbuff[128] = 0; + printm(M_BARE, " 574 - 128- AppId : %s\n", pbuff); + memcpy(pbuff, buffer + 702, 37); + pbuff[37] = 0; + printm(M_BARE, " 702 - 37- CpyFile: %s\n", pbuff); + memcpy(pbuff, buffer + 739, 37); + pbuff[37] = 0; + printm(M_BARE, " 739 - 37- AbsFile: %s\n", pbuff); + memcpy(pbuff, buffer + 776, 37); + pbuff[37] = 0; + printm(M_BARE, " 776 - 37- BibFile: %s\n", pbuff); + printm(M_BARE, " 813 - 17- DTCreat: %s\n", format_date(&buffer[813])); + printm(M_BARE, " 830 - 17- DTModif: %s\n", format_date(&buffer[830])); + printm(M_BARE, " 847 - 17- DTExpir: %s\n", format_date(&buffer[847])); + printm(M_BARE, " 864 - 17- DTEffec: %s\n", format_date(&buffer[864])); + + printm(M_BARE, "Root record:\n"); + show_head_entry(); + show_entry((DirEntry *) &(buffer[156])); + + return 1; +} + +int show_iso_infos(int f_iso) { + unsigned char buffer[2048]; + char pbuff[130]; + short int s, nogood = 0; + + read_sector(f_iso, buffer, GUESS, 16); + + printm(M_BARE, "Sector guessed mode : %s\n", sec_modes[guess_type(f_iso, 16)]); + printm(M_BARE, "offset-size-info : contents\n"); + printm(M_BARE, " 0 - 1- 1 : %i\n", buffer[0]); + 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, "(*this was the signature*)\n"); + if (buffer[0] != 1) nogood = 1; + if (strcmp(pbuff, "CD001")) nogood = 1; + if (s != 1) nogood = 1; + + if (nogood) { + printm(M_BARE, "Not a valid iso9660 file.\n"); + return 0; + } + + memcpy(pbuff, buffer + 8, 32); + pbuff[32] = 0; + printm(M_BARE, " 8 - 32- SYSID : %s\n", pbuff); + 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", *((short int *) &(buffer[140]))); + printm(M_BARE, " 144 - 4- 2SLPath: %i\n", *((short int *) &(buffer[144]))); + printm(M_BARE, " 148 - 4- 1SBPath: %i\n", swap_dword(*((short int *) &(buffer[148])))); + printm(M_BARE, " 152 - 4- 2SBPath: %i\n", swap_dword(*((short int *) &(buffer[152])))); + memcpy(pbuff, buffer + 190, 128); + pbuff[128] = 0; + printm(M_BARE, " 190 - 128- VStId : %s\n", pbuff); + memcpy(pbuff, buffer + 318, 128); + pbuff[128] = 0; + printm(M_BARE, " 318 - 128- PubId : %s\n", pbuff); + memcpy(pbuff, buffer + 446, 128); + pbuff[128] = 0; + printm(M_BARE, " 446 - 128- DPrId : %s\n", pbuff); + memcpy(pbuff, buffer + 574, 128); + pbuff[128] = 0; + printm(M_BARE, " 574 - 128- AppId : %s\n", pbuff); + memcpy(pbuff, buffer + 702, 37); + pbuff[37] = 0; + printm(M_BARE, " 702 - 37- CpyFile: %s\n", pbuff); + memcpy(pbuff, buffer + 739, 37); + pbuff[37] = 0; + printm(M_BARE, " 739 - 37- AbsFile: %s\n", pbuff); + memcpy(pbuff, buffer + 776, 37); + pbuff[37] = 0; + printm(M_BARE, " 776 - 37- BibFile: %s\n", pbuff); + printm(M_BARE, " 813 - 17- DTCreat: %s\n", format_date(&buffer[813])); + printm(M_BARE, " 830 - 17- DTModif: %s\n", format_date(&buffer[830])); + printm(M_BARE, " 847 - 17- DTExpir: %s\n", format_date(&buffer[847])); + printm(M_BARE, " 864 - 17- DTEffec: %s\n", format_date(&buffer[864])); + + printm(M_BARE, "Root record:\n"); + show_head_entry(); + show_entry((DirEntry *) &(buffer[156])); + + return 1; +} + +int get_iso_infos(FILE * f_iso) { + unsigned char buffer[2048]; + char pbuff[130]; + short int s, nogood = 0; + + read_sector(f_iso, buffer, GUESS, 16); + + memcpy(pbuff, buffer + 1, 5); + pbuff[5] = 0; + + s = *((short int *) &(buffer[6])); + if (buffer[0] != 1) nogood = 1; + if (strcmp(pbuff, "CD001")) nogood = 1; + if (s != 1) nogood = 1; + + if (nogood) { + printm(M_ERROR, "Not a valid iso9660 file.\n"); + return 0; + } + + pt1 = *((short int *) &(buffer[140])); + pt2 = *((short int *) &(buffer[144])); + snum = *((int *) &(buffer[80])); + ptl = *((long int *) &(buffer[132])); + rootDir = *((struct DirEntry *) &(buffer[156])); + return 1; +} + +int get_iso_infos(int f_iso) { + unsigned char buffer[2048]; + char pbuff[130]; + short int s, nogood = 0; + + read_sector(f_iso, buffer, GUESS, 16); + + memcpy(pbuff, buffer + 1, 5); + pbuff[5] = 0; + + s = *((short int *) &(buffer[6])); + if (buffer[0] != 1) nogood = 1; + if (strcmp(pbuff, "CD001")) nogood = 1; + if (s != 1) nogood = 1; + + if (nogood) { + printm(M_ERROR, "Not a valid iso9660 file.\n"); + return 0; + } + + pt1 = *((short int *) &(buffer[140])); + pt2 = *((short int *) &(buffer[144])); + snum = *((int *) &(buffer[80])); + ptl = *((long int *) &(buffer[132])); + rootDir = *((struct DirEntry *) &(buffer[156])); + return 1; +} + +int get_pt_infos(FILE * f_iso) { + unsigned char * buffer; + + if ((pt1 <= 0) && (pt2 <= 0)) + if (!get_iso_infos(f_iso)) + return 0; + + if ((!pt1) & (!pt2)) { + printm(M_ERROR, "No path table defined.\n"); + return 0; + } + + buffer = (unsigned char *) malloc(ptl); + read_datas(f_iso, buffer, GUESS, !pt1 ? pt2 : pt1, ptl); + + if (buffer[0] == 1) + if (buffer[1] == 0) + if (buffer[6] == 1) + if (buffer[7] == 0) + if (buffer[8] == 0) + if (buffer[9] == 0) + root = *((unsigned long int *) &(buffer[2])); + + free(buffer); + return root ? 1 : 0; +} + +int show_pt_infos(FILE * f_iso) { + unsigned char * buffer; + char pbuf[100]; + int i, ptr; + + if ((pt1 <= 0) && (pt2 <= 0)) + if (!get_iso_infos(f_iso)) + return 0; + + if ((!pt1) & (!pt2)) { + printm(M_ERROR, "No path table defined.\n"); + return 0; + } + + buffer = (unsigned char *) malloc(ptl); + read_datas(f_iso, buffer, GUESS, !pt1 ? pt2 : pt1, ptl); + + 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); + ptr += 8 + buffer[ptr]; + } + + free(buffer); + return 1; +} + +int show_pt_infos(int f_iso) { + unsigned char * buffer; + char pbuf[100]; + int i, ptr; + + if ((pt1 <= 0) && (pt2 <= 0)) + if (!get_iso_infos(f_iso)) + return 0; + + if ((!pt1) & (!pt2)) { + printm(M_ERROR, "No path table defined.\n"); + return 0; + } + + buffer = (unsigned char *) malloc(ptl); + read_datas(f_iso, buffer, GUESS, !pt1 ? pt2 : pt1, ptl); + + 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); + ptr += 8 + buffer[ptr]; + } + + free(buffer); + return 1; +} + +struct DirEntry find_path(FILE * f_iso, char * path) { + char ** pts = split(path, '/'); + struct DirEntry dir = {0, 0, 0, 0, 0}; + + if ((pt1 <= 0) && (pt2 <= 0)) + if (!get_iso_infos(f_iso)) + return dir; + + if ((!pt1) & (!pt2)) { + printm(M_ERROR, "No path table defined.\n"); + return dir; + } + + if (!**pts) + pts++; + + for (dir = rootDir; *pts; pts++) { + if (!strlen(*pts)) + continue; + dir = find_dir_entry(f_iso, &dir, *pts); + if (!dir.R) + return dir; + } + + return dir; +} + +struct DirEntry find_path(int f_iso, char * path) { + char ** pts = split(path, '/'); + struct DirEntry dir = {0, 0, 0, 0, 0}; + + if ((pt1 <= 0) && (pt2 <= 0)) + if (!get_iso_infos(f_iso)) + return dir; + + if ((!pt1) & (!pt2)) { + printm(M_ERROR, "No path table defined.\n"); + return dir; + } + + if (!**pts) + pts++; + + for (dir = rootDir; *pts; pts++) { + if (!strlen(*pts)) + continue; + dir = find_dir_entry(f_iso, &dir, *pts); + if (!dir.R) + return dir; + } + + return dir; +} + +struct DirEntry find_parent(FILE * f_iso, char * path) { + char ** pts, * p; + struct DirEntry dir = {0, 0, 0, 0, 0}; + + if ((p = strchr(path, '/'))) { + *p = 0; + } + if (!*path) { + return rootDir; + } + pts = split(path, '/'); + + if ((pt1 <= 0) && (pt2 <= 0)) + if (!get_iso_infos(f_iso)) + return dir; + + if ((!pt1) & (!pt2)) { + printm(M_ERROR, "No path table defined.\n"); + return dir; + } + + if (!**pts) + pts++; + + for (dir = rootDir; *pts; pts++) { + if (!strlen(*pts)) + continue; + dir = find_dir_entry(f_iso, &dir, *pts); + if (!dir.R) + return dir; + } + + return dir; +} + +struct DirEntry find_parent(int f_iso, char * path) { + char ** pts, * p; + struct DirEntry dir = {0, 0, 0, 0, 0}; + + if ((p = strchr(path, '/'))) { + *p = 0; + } + if (!*path) { + return rootDir; + } + pts = split(path, '/'); + + if ((pt1 <= 0) && (pt2 <= 0)) + if (!get_iso_infos(f_iso)) + return dir; + + if ((!pt1) & (!pt2)) { + printm(M_ERROR, "No path table defined.\n"); + return dir; + } + + if (!**pts) + pts++; + + for (dir = rootDir; *pts; pts++) { + if (!strlen(*pts)) + continue; + dir = find_dir_entry(f_iso, &dir, *pts); + if (!dir.R) + return dir; + } + + return dir; +} diff --git a/lib/crctable.out b/lib/crctable.out new file mode 100644 index 0000000..47842cd --- /dev/null +++ b/lib/crctable.out @@ -0,0 +1,91 @@ +/*****************************************************************/ +/* */ +/* CRC LOOKUP TABLE */ +/* ================ */ +/* The following CRC lookup table was generated automagically */ +/* by the Rocksoft^tm Model CRC Algorithm Table Generation */ +/* Program V1.0 using the following model parameters: */ +/* */ +/* Width : 4 bytes. */ +/* Poly : 0x8001801BL */ +/* Reverse : TRUE. */ +/* */ +/* For more information on the Rocksoft^tm Model CRC Algorithm, */ +/* see the document titled "A Painless Guide to CRC Error */ +/* Detection Algorithms" by Ross Williams */ +/* (ross@guest.adelaide.edu.au.). This document is likely to be */ +/* in the FTP archive "ftp.adelaide.edu.au/pub/rocksoft". */ +/* */ +/*****************************************************************/ + +unsigned long EDC_crctable[256] = +{ + 0x00000000L, 0x90910101L, 0x91210201L, 0x01B00300L, + 0x92410401L, 0x02D00500L, 0x03600600L, 0x93F10701L, + 0x94810801L, 0x04100900L, 0x05A00A00L, 0x95310B01L, + 0x06C00C00L, 0x96510D01L, 0x97E10E01L, 0x07700F00L, + 0x99011001L, 0x09901100L, 0x08201200L, 0x98B11301L, + 0x0B401400L, 0x9BD11501L, 0x9A611601L, 0x0AF01700L, + 0x0D801800L, 0x9D111901L, 0x9CA11A01L, 0x0C301B00L, + 0x9FC11C01L, 0x0F501D00L, 0x0EE01E00L, 0x9E711F01L, + 0x82012001L, 0x12902100L, 0x13202200L, 0x83B12301L, + 0x10402400L, 0x80D12501L, 0x81612601L, 0x11F02700L, + 0x16802800L, 0x86112901L, 0x87A12A01L, 0x17302B00L, + 0x84C12C01L, 0x14502D00L, 0x15E02E00L, 0x85712F01L, + 0x1B003000L, 0x8B913101L, 0x8A213201L, 0x1AB03300L, + 0x89413401L, 0x19D03500L, 0x18603600L, 0x88F13701L, + 0x8F813801L, 0x1F103900L, 0x1EA03A00L, 0x8E313B01L, + 0x1DC03C00L, 0x8D513D01L, 0x8CE13E01L, 0x1C703F00L, + 0xB4014001L, 0x24904100L, 0x25204200L, 0xB5B14301L, + 0x26404400L, 0xB6D14501L, 0xB7614601L, 0x27F04700L, + 0x20804800L, 0xB0114901L, 0xB1A14A01L, 0x21304B00L, + 0xB2C14C01L, 0x22504D00L, 0x23E04E00L, 0xB3714F01L, + 0x2D005000L, 0xBD915101L, 0xBC215201L, 0x2CB05300L, + 0xBF415401L, 0x2FD05500L, 0x2E605600L, 0xBEF15701L, + 0xB9815801L, 0x29105900L, 0x28A05A00L, 0xB8315B01L, + 0x2BC05C00L, 0xBB515D01L, 0xBAE15E01L, 0x2A705F00L, + 0x36006000L, 0xA6916101L, 0xA7216201L, 0x37B06300L, + 0xA4416401L, 0x34D06500L, 0x35606600L, 0xA5F16701L, + 0xA2816801L, 0x32106900L, 0x33A06A00L, 0xA3316B01L, + 0x30C06C00L, 0xA0516D01L, 0xA1E16E01L, 0x31706F00L, + 0xAF017001L, 0x3F907100L, 0x3E207200L, 0xAEB17301L, + 0x3D407400L, 0xADD17501L, 0xAC617601L, 0x3CF07700L, + 0x3B807800L, 0xAB117901L, 0xAAA17A01L, 0x3A307B00L, + 0xA9C17C01L, 0x39507D00L, 0x38E07E00L, 0xA8717F01L, + 0xD8018001L, 0x48908100L, 0x49208200L, 0xD9B18301L, + 0x4A408400L, 0xDAD18501L, 0xDB618601L, 0x4BF08700L, + 0x4C808800L, 0xDC118901L, 0xDDA18A01L, 0x4D308B00L, + 0xDEC18C01L, 0x4E508D00L, 0x4FE08E00L, 0xDF718F01L, + 0x41009000L, 0xD1919101L, 0xD0219201L, 0x40B09300L, + 0xD3419401L, 0x43D09500L, 0x42609600L, 0xD2F19701L, + 0xD5819801L, 0x45109900L, 0x44A09A00L, 0xD4319B01L, + 0x47C09C00L, 0xD7519D01L, 0xD6E19E01L, 0x46709F00L, + 0x5A00A000L, 0xCA91A101L, 0xCB21A201L, 0x5BB0A300L, + 0xC841A401L, 0x58D0A500L, 0x5960A600L, 0xC9F1A701L, + 0xCE81A801L, 0x5E10A900L, 0x5FA0AA00L, 0xCF31AB01L, + 0x5CC0AC00L, 0xCC51AD01L, 0xCDE1AE01L, 0x5D70AF00L, + 0xC301B001L, 0x5390B100L, 0x5220B200L, 0xC2B1B301L, + 0x5140B400L, 0xC1D1B501L, 0xC061B601L, 0x50F0B700L, + 0x5780B800L, 0xC711B901L, 0xC6A1BA01L, 0x5630BB00L, + 0xC5C1BC01L, 0x5550BD00L, 0x54E0BE00L, 0xC471BF01L, + 0x6C00C000L, 0xFC91C101L, 0xFD21C201L, 0x6DB0C300L, + 0xFE41C401L, 0x6ED0C500L, 0x6F60C600L, 0xFFF1C701L, + 0xF881C801L, 0x6810C900L, 0x69A0CA00L, 0xF931CB01L, + 0x6AC0CC00L, 0xFA51CD01L, 0xFBE1CE01L, 0x6B70CF00L, + 0xF501D001L, 0x6590D100L, 0x6420D200L, 0xF4B1D301L, + 0x6740D400L, 0xF7D1D501L, 0xF661D601L, 0x66F0D700L, + 0x6180D800L, 0xF111D901L, 0xF0A1DA01L, 0x6030DB00L, + 0xF3C1DC01L, 0x6350DD00L, 0x62E0DE00L, 0xF271DF01L, + 0xEE01E001L, 0x7E90E100L, 0x7F20E200L, 0xEFB1E301L, + 0x7C40E400L, 0xECD1E501L, 0xED61E601L, 0x7DF0E700L, + 0x7A80E800L, 0xEA11E901L, 0xEBA1EA01L, 0x7B30EB00L, + 0xE8C1EC01L, 0x7850ED00L, 0x79E0EE00L, 0xE971EF01L, + 0x7700F000L, 0xE791F101L, 0xE621F201L, 0x76B0F300L, + 0xE541F401L, 0x75D0F500L, 0x7460F600L, 0xE4F1F701L, + 0xE381F801L, 0x7310F900L, 0x72A0FA00L, 0xE231FB01L, + 0x71C0FC00L, 0xE151FD01L, 0xE0E1FE01L, 0x7070FF00L +}; + +/*****************************************************************/ +/* End of CRC Lookup Table */ +/*****************************************************************/ diff --git a/lib/crctables b/lib/crctables new file mode 100644 index 0000000..3dd89c1 --- /dev/null +++ b/lib/crctables @@ -0,0 +1,142 @@ +static unsigned char rs_l12_alog[255] = { + 1, 2, 4, 8,16,32,64,128,29,58,116,232,205,135,19,38,76,152,45,90,180,117,234,201,143, 3, 6,12,24,48,96,192,157,39,78,156,37,74,148,53,106,212,181,119,238,193,159,35,70,140, 5,10,20,40,80,160,93,186,105,210,185,111,222,161,95,190,97,194,153,47,94,188,101,202,137,15,30,60,120,240,253,231,211,187,107,214,177,127,254,225,223,163,91,182,113,226,217,175,67,134,17,34,68,136,13,26,52,104,208,189,103,206,129,31,62,124,248,237,199,147,59,118,236,197,151,51,102,204,133,23,46,92,184,109,218,169,79,158,33,66,132,21,42,84,168,77,154,41,82,164,85,170,73,146,57,114,228,213,183,115,230,209,191,99,198,145,63,126,252,229,215,179,123,246,241,255,227,219,171,75,150,49,98,196,149,55,110,220,165,87,174,65,130,25,50,100,200,141, 7,14,28,56,112,224,221,167,83,166,81,162,89,178,121,242,249,239,195,155,43,86,172,69,138, 9,18,36,72,144,61,122,244,245,247,243,251,235,203,139,11,22,44,88,176,125,250,233,207,131,27,54,108,216,173,71,142,}; +static unsigned char rs_l12_log[256] = { + 0, 0, 1,25, 2,50,26,198, 3,223,51,238,27,104,199,75, 4,100,224,14,52,141,239,129,28,193,105,248,200, 8,76,113, 5,138,101,47,225,36,15,33,53,147,142,218,240,18,130,69,29,181,194,125,106,39,249,185,201,154, 9,120,77,228,114,166, 6,191,139,98,102,221,48,253,226,152,37,179,16,145,34,136,54,208,148,206,143,150,219,189,241,210,19,92,131,56,70,64,30,66,182,163,195,72,126,110,107,58,40,84,250,133,186,61,202,94,155,159,10,21,121,43,78,212,229,172,115,243,167,87, 7,112,192,247,140,128,99,13,103,74,222,237,49,197,254,24,227,165,153,119,38,184,180,124,17,68,146,217,35,32,137,46,55,63,209,91,149,188,207,205,144,135,151,178,220,252,190,97,242,86,211,171,20,42,93,158,132,60,57,83,71,109,65,162,31,45,67,216,183,123,164,118,196,23,73,236,127,12,111,246,108,161,59,82,41,157,85,170,251,96,134,177,187,204,62,90,203,89,95,176,156,169,160,81,11,245,22,235,122,117,44,215,79,174,213,233,230,231,173,232,116,214,244,234,168,80,88,175,}; +static unsigned char rs_sub_rw_alog[63] = { + 1, 2, 4, 8,16,32, 3, 6,12,24,48,35, 5,10,20,40,19,38,15,30,60,59,53,41,17,34, 7,14,28,56,51,37, 9,18,36,11,22,44,27,54,47,29,58,55,45,25,50,39,13,26,52,43,21,42,23,46,31,62,63,61,57,49,33,}; +static unsigned char rs_sub_rw_log[63] = { + 0, 0, 1, 6, 2,12, 7,26, 3,32,13,35, 8,48,27,18, 4,24,33,16,14,52,36,54, 9,45,49,38,28,41,19,56, 5,62,25,11,34,31,17,47,15,23,53,51,37,44,55,40,10,61,46,30,50,22,39,43,29,60,42,21,20,59,57,}; +static unsigned char SQ[2][2] = { +{26,6,}, +{7,1,}, +}; +static unsigned char SP[4][20] = { +{57,38,44,29,17,57,53,58,60,39,12,38,18,41,6,25,39,37,5,18,}, +{38,62,42,13,30,11,46,5,54,26,12,49,48,46,8,50,28,9,12,39,}, +{32,18,41,49,52,62,38,36,39,58,37,24,34,51,51,27,28,36,22,21,}, +{44,50,35,23,0,59,1,3,45,18,44,24,47,12,31,45,43,11,24,6,}, +}; +static unsigned char AQ[4][24] = { +{58,152,173,95,88,43,134,205,143,131,163,75,37,109,194,159,168,227,153,59,101,}, +{30,214,148,138,112,154,157,96,49,198,189,249,83,23,70,237,70,41,47,52,125,247,}, +{162,244,13,171,213,236,71,177,253,162,59,78,46,68,238,112,147,197,115,200,117,15,236,}, +{158,179,101,94,49,140,211,149,137,169,81,6,33,30,27,24,21,18,15,12,9,6,3,0,}, +}; +static unsigned char AP[4][32] = { +{140,143,52,103,249,142,180,197,5,155,153,132,143,244,101,76,102,155,203,104,58,152,173,95,88,43,134,205,143,131,163,75,}, +{104,97,17,162,205,252,218,199,202,41,136,106,119,238,193,103,123,242,83,178,30,214,148,138,112,154,157,96,49,198,189,249,}, +{240,119,29,185,67,11,131,40,7,41,80,147,151,17,245,253,208,66,228,116,162,244,13,171,213,236,71,177,253,162,59,78,}, +{149,58,109,0,148,186,203,11,161,159,138,149,250,107,82,108,161,209,110,64,158,179,101,94,49,140,211,149,137,169,81,6,}, +}; +static unsigned char DQ[2][43] = { +{190,96,250,132,59,81,159,154,200,7,111,245,10,20,41,156,168,79,173,231,229,171,210,240,17,67,215,43,120,8,199,74,102,220,251,95,175,87,166,113,75,198,25,}, +{97,251,133,60,82,160,155,201,8,112,246,11,21,42,157,169,80,174,232,230,172,211,241,18,68,216,44,121,9,200,75,103,221,252,96,176,88,167,114,76,199,26,1,}, +}; +static unsigned char DP[2][24] = { +{231,229,171,210,240,17,67,215,43,120,8,199,74,102,220,251,95,175,87,166,113,75,198,25,}, +{230,172,211,241,18,68,216,44,121,9,200,75,103,221,252,96,176,88,167,114,76,199,26,1,}, +}; +static unsigned char yellowbook_scrambler[2340] = { + 1,128,0,96,0,40,0,30,128,8,96,6,168,2,254,129,128,96,96,40,40,30,158, + 136,104,102,174,170,252,127,1,224,0,72,0,54,128,22,224,14,200,4,86,131,126,225, + 224,72,72,54,182,150,246,238,198,204,82,213,253,159,1,168,0,126,128,32,96,24,40, + 10,158,135,40,98,158,169,168,126,254,160,64,120,48,34,148,25,175,74,252,55,1,214, + 128,94,224,56,72,18,182,141,182,229,182,203,54,215,86,222,190,216,112,90,164,59,59, + 83,83,125,253,225,129,136,96,102,168,42,254,159,0,104,0,46,128,28,96,9,232,6, + 206,130,212,97,159,104,104,46,174,156,124,105,225,238,200,76,86,181,254,247,0,70,128, + 50,224,21,136,15,38,132,26,227,75,9,247,70,198,178,210,245,157,135,41,162,158,249, + 168,66,254,177,128,116,96,39,104,26,174,139,60,103,81,234,188,79,49,244,20,71,79, + 114,180,37,183,91,54,187,86,243,126,197,224,83,8,61,198,145,146,236,109,141,237,165, + 141,187,37,179,91,53,251,87,3,126,129,224,96,72,40,54,158,150,232,110,206,172,84, + 125,255,97,128,40,96,30,168,8,126,134,160,98,248,41,130,158,225,168,72,126,182,160, + 118,248,38,194,154,209,171,28,127,73,224,54,200,22,214,142,222,228,88,75,122,183,99, + 54,169,214,254,222,192,88,80,58,188,19,49,205,212,85,159,127,40,32,30,152,8,106, + 134,175,34,252,25,129,202,224,87,8,62,134,144,98,236,41,141,222,229,152,75,42,183, + 95,54,184,22,242,142,197,164,83,59,125,211,97,157,232,105,142,174,228,124,75,97,247, + 104,70,174,178,252,117,129,231,32,74,152,55,42,150,159,46,232,28,78,137,244,102,199, + 106,210,175,29,188,9,177,198,244,82,199,125,146,161,173,184,125,178,161,181,184,119,50, + 166,149,186,239,51,12,21,197,207,19,20,13,207,69,148,51,47,85,220,63,25,208,10, + 220,7,25,194,138,209,167,28,122,137,227,38,201,218,214,219,30,219,72,91,118,187,102, + 243,106,197,239,19,12,13,197,197,147,19,45,205,221,149,153,175,42,252,31,1,200,0, + 86,128,62,224,16,72,12,54,133,214,227,30,201,200,86,214,190,222,240,88,68,58,179, + 83,53,253,215,1,158,128,104,96,46,168,28,126,137,224,102,200,42,214,159,30,232,8, + 78,134,180,98,247,105,134,174,226,252,73,129,246,224,70,200,50,214,149,158,239,40,76, + 30,181,200,119,22,166,142,250,228,67,11,113,199,100,82,171,125,191,97,176,40,116,30, + 167,72,122,182,163,54,249,214,194,222,209,152,92,106,185,239,50,204,21,149,207,47,20, + 28,15,73,196,54,211,86,221,254,217,128,90,224,59,8,19,70,141,242,229,133,139,35, + 39,89,218,186,219,51,27,85,203,127,23,96,14,168,4,126,131,96,97,232,40,78,158, + 180,104,119,110,166,172,122,253,227,1,137,192,102,208,42,220,31,25,200,10,214,135,30, + 226,136,73,166,182,250,246,195,6,209,194,220,81,153,252,106,193,239,16,76,12,53,197, + 215,19,30,141,200,101,150,171,46,255,92,64,57,240,18,196,13,147,69,173,243,61,133, + 209,163,28,121,201,226,214,201,158,214,232,94,206,184,84,114,191,101,176,43,52,31,87, + 72,62,182,144,118,236,38,205,218,213,155,31,43,72,31,118,136,38,230,154,202,235,23, + 15,78,132,52,99,87,105,254,174,192,124,80,33,252,24,65,202,176,87,52,62,151,80, + 110,188,44,113,221,228,89,139,122,231,99,10,169,199,62,210,144,93,172,57,189,210,241, + 157,132,105,163,110,249,236,66,205,241,149,132,111,35,108,25,237,202,205,151,21,174,143, + 60,100,17,235,76,79,117,244,39,7,90,130,187,33,179,88,117,250,167,3,58,129,211, + 32,93,216,57,154,146,235,45,143,93,164,57,187,82,243,125,133,225,163,8,121,198,162, + 210,249,157,130,233,161,142,248,100,66,171,113,191,100,112,43,100,31,107,72,47,118,156, + 38,233,218,206,219,20,91,79,123,116,35,103,89,234,186,207,51,20,21,207,79,20,52, + 15,87,68,62,179,80,117,252,39,1,218,128,91,32,59,88,19,122,141,227,37,137,219, + 38,219,90,219,123,27,99,75,105,247,110,198,172,82,253,253,129,129,160,96,120,40,34, + 158,153,168,106,254,175,0,124,0,33,192,24,80,10,188,7,49,194,148,81,175,124,124, + 33,225,216,72,90,182,187,54,243,86,197,254,211,0,93,192,57,144,18,236,13,141,197, + 165,147,59,45,211,93,157,249,169,130,254,225,128,72,96,54,168,22,254,142,192,100,80, + 43,124,31,97,200,40,86,158,190,232,112,78,164,52,123,87,99,126,169,224,126,200,32, + 86,152,62,234,144,79,44,52,29,215,73,158,182,232,118,206,166,212,122,223,99,24,41, + 202,158,215,40,94,158,184,104,114,174,165,188,123,49,227,84,73,255,118,192,38,208,26, + 220,11,25,199,74,210,183,29,182,137,182,230,246,202,198,215,18,222,141,152,101,170,171, + 63,63,80,16,60,12,17,197,204,83,21,253,207,1,148,0,111,64,44,48,29,212,9, + 159,70,232,50,206,149,148,111,47,108,28,45,201,221,150,217,174,218,252,91,1,251,64, + 67,112,49,228,20,75,79,119,116,38,167,90,250,187,3,51,65,213,240,95,4,56,3, + 82,129,253,160,65,184,48,114,148,37,175,91,60,59,81,211,124,93,225,249,136,66,230, + 177,138,244,103,7,106,130,175,33,188,24,113,202,164,87,59,126,147,96,109,232,45,142, + 157,164,105,187,110,243,108,69,237,243,13,133,197,163,19,57,205,210,213,157,159,41,168, + 30,254,136,64,102,176,42,244,31,7,72,2,182,129,182,224,118,200,38,214,154,222,235, + 24,79,74,180,55,55,86,150,190,238,240,76,68,53,243,87,5,254,131,0,97,192,40, + 80,30,188,8,113,198,164,82,251,125,131,97,161,232,120,78,162,180,121,183,98,246,169, + 134,254,226,192,73,144,54,236,22,205,206,213,148,95,47,120,28,34,137,217,166,218,250, + 219,3,27,65,203,112,87,100,62,171,80,127,124,32,33,216,24,90,138,187,39,51,90, + 149,251,47,3,92,1,249,192,66,208,49,156,20,105,207,110,212,44,95,93,248,57,130, + 146,225,173,136,125,166,161,186,248,115,2,165,193,187,16,115,76,37,245,219,7,27,66, + 139,113,167,100,122,171,99,63,105,208,46,220,28,89,201,250,214,195,30,209,200,92,86, + 185,254,242,192,69,144,51,44,21,221,207,25,148,10,239,71,12,50,133,213,163,31,57, + 200,18,214,141,158,229,168,75,62,183,80,118,188,38,241,218,196,91,19,123,77,227,117, + 137,231,38,202,154,215,43,30,159,72,104,54,174,150,252,110,193,236,80,77,252,53,129, + 215,32,94,152,56,106,146,175,45,188,29,177,201,180,86,247,126,198,160,82,248,61,130, + 145,161,172,120,125,226,161,137,184,102,242,170,197,191,19,48,13,212,5,159,67,40,49, + 222,148,88,111,122,172,35,61,217,209,154,220,107,25,239,74,204,55,21,214,143,30,228, + 8,75,70,183,114,246,165,134,251,34,195,89,145,250,236,67,13,241,197,132,83,35,125, + 217,225,154,200,107,22,175,78,252,52,65,215,112,94,164,56,123,82,163,125,185,225,178, + 200,117,150,167,46,250,156,67,41,241,222,196,88,83,122,189,227,49,137,212,102,223,106, + 216,47,26,156,11,41,199,94,210,184,93,178,185,181,178,247,53,134,151,34,238,153,140, + 106,229,239,11,12,7,69,194,179,17,181,204,119,21,230,143,10,228,7,11,66,135,113, + 162,164,121,187,98,243,105,133,238,227,12,73,197,246,211,6,221,194,217,145,154,236,107, + 13,239,69,140,51,37,213,219,31,27,72,11,118,135,102,226,170,201,191,22,240,14,196, + 4,83,67,125,241,225,132,72,99,118,169,230,254,202,192,87,16,62,140,16,101,204,43, + 21,223,79,24,52,10,151,71,46,178,156,117,169,231,62,202,144,87,44,62,157,208,105, + 156,46,233,220,78,217,244,90,199,123,18,163,77,185,245,178,199,53,146,151,45,174,157, + 188,105,177,238,244,76,71,117,242,167,5,186,131,51,33,213,216,95,26,184,11,50,135, + 85,162,191,57,176,18,244,13,135,69,162,179,57,181,210,247,29,134,137,162,230,249,138, + 194,231,17,138,140,103,37,234,155,15,43,68,31,115,72,37,246,155,6,235,66,207,113, + 148,36,111,91,108,59,109,211,109,157,237,169,141,190,229,176,75,52,55,87,86,190,190, + 240,112,68,36,51,91,85,251,127,3,96,1,232,0,78,128,52,96,23,104,14,174,132, + 124,99,97,233,232,78,206,180,84,119,127,102,160,42,248,31,2,136,1,166,128,122,224, + 35,8,25,198,138,210,231,29,138,137,167,38,250,154,195,43,17,223,76,88,53,250,151, + 3,46,129,220,96,89,232,58,206,147,20,109,207,109,148,45,175,93,188,57,177,210,244, + 93,135,121,162,162,249,185,130,242,225,133,136,99,38,169,218,254,219,0,91,64,59,112, + 19,100,13,235,69,143,115,36,37,219,91,27,123,75,99,119,105,230,174,202,252,87,1, + 254,128,64,96,48,40,20,30,143,72,100,54,171,86,255,126,192,32,80,24,60,10,145, + 199,44,82,157,253,169,129,190,224,112,72,36,54,155,86,235,126,207,96,84,40,63,94, + 144,56,108,18,173,205,189,149,177,175,52,124,23,97,206,168,84,126,191,96,112,40,36, + 30,155,72,107,118,175,102,252,42,193,223,16,88,12,58,133,211,35,29,217,201,154,214, + 235,30,207,72,84,54,191,86,240,62,196,16,83,76,61,245,209,135,28,98,137,233,166, + 206,250,212,67,31,113,200,36,86,155,126,235,96,79,104,52,46,151,92,110,185,236,114, + 205,229,149,139,47,39,92,26,185,203,50,215,85,158,191,40,112,30,164,8,123,70,163, + 114,249,229,130,203,33,151,88,110,186,172,115,61,229,209,139,28,103,73,234,182,207,54, + 212,22,223,78,216,52,90,151,123,46,163,92,121,249,226,194,201,145,150,236,110,205,236, + 85,141,255,37,128,27,32,11,88,7,122,130,163,33,185,216,114,218,165,155,59,43,83, + 95,125,248,33,130,152,97,170,168,127,62,160,16,120,12,34,133,217,163,26,249,203,2, + 215,65,158,176,104,116,46,167,92,122,185,227,50,201,213,150,223,46,216,28,90,137,251, + 38,195,90,209,251,28,67,73,241,246,196,70,211,114,221,229,153, +}; diff --git a/lib/dteutils.cpp b/lib/dteutils.cpp new file mode 100644 index 0000000..4ae8668 --- /dev/null +++ b/lib/dteutils.cpp @@ -0,0 +1,342 @@ +/* + * PSX-Tools Bundle Pack + * Copyright (C) 2002 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include "fileutils.h" +#include "generic.h" + +class thingtree { + public: + static void addstring(const char *, long); + static long look(const char *); + static void destroy(); + private: + thingtree(); + thingtree(char, bool, thingtree *, long); + ~thingtree(); + char thischar; + bool terminal; + thingtree * father, * child, * brother; + long thingentry; + static thingtree * root; +}; + +thingtree * thingtree::root = 0; + +thingtree::thingtree(char gchar, bool gterm, thingtree * gfather, long gthingentry) : thischar(gchar), terminal(gterm), thingentry(gthingentry) { + if (!gfather) { + if (!root) { + root = new thingtree(); + } + father = root; + } else { + father = gfather; + } + brother = father->child; + father->child = this; +} + +thingtree::thingtree() : thischar(0), terminal(false), father(0), child(0), brother(0) { } + +long thingtree::look(const char * s) { + const char * p; + long currentthing = -1; + thingtree * ptr = root; + + if (!ptr) { + printm(M_ERROR, "Error: thingtree not initialised\n"); + exit(-1); + } + + for (p = s; *p; p++) { +// printm(M_INFO, "Looking for '%c'\n", *p); + for (ptr = ptr->child; ptr; ptr = ptr->brother) { +// printm(M_INFO, "Looking in %p: %c\n", ptr, ptr->thischar); + if (ptr->thischar == *p) { + if (ptr->terminal) { + currentthing = ptr->thingentry; + } + break; + } + } + if (!ptr) { +// printm(M_INFO, "Not found.\n"); + break; + } + } + + if (currentthing == -1) { + printm(M_ERROR, "Error, can't find any entry for string '%s'\n", s); + } + + return currentthing; +} + +void thingtree::addstring(const char * s, long thingentry) { + const char * p; + thingtree * ptr, * fptr; + + if (!root) { + root = new thingtree(); + } + +// printm(M_INFO, "Creating new thingtree entry: %li='%s'\n", thingentry, s); + + ptr = root; + for (p = s; *p; p++) { + fptr = ptr; +// printm(M_INFO, "Finding entry for '%c'\n", *p); + for (ptr = ptr->child; ptr; ptr = ptr->brother) { +// printm(M_INFO, "Browsing childs: %p = %c\n", ptr, ptr->thischar); + if (ptr->thischar == *p) + break; + } + + if (!ptr) { +// printm(M_INFO, "Creating new branch for %c\n", *p); + ptr = new thingtree(*p, *(p + 1) == 0, fptr, thingentry); +// printm(M_INFO, "Created branch %p\n", ptr); + } else { + if (*(p + 1) == 0) { + ptr->terminal = true; + ptr->thingentry = thingentry; + } + } + } +} + +void thingtree::destroy() { + delete root; + root = 0; +} + +thingtree::~thingtree() { + if (father->child == this) + father->child = brother; + + while (child) + delete child; +} + +char * things[256]; + +char * dte_text; +char dte_flags[256 * 256]; +long dte_counters[256 * 256]; +long dte_entries[256]; +char alloweds[256]; +long dte_usage[256]; + +long dte_most; +long dte_counter; +long dte_text_size; +int dte_size; +long gain; +long nb_dte = 0; +long tnb_dte = 0; + +void dte_reset(void) { + memset(dte_counters, 0, 0x40000); + dte_most = 0; + dte_counter = 0; +} + +void build_dte(void) { + int i; + unsigned short t, t2; + unsigned short p = 0; + + for (i = 0; i < dte_text_size; i++) { + t = *((unsigned short *) (dte_text + i)); + t2 = *((unsigned short *) (dte_text + i + 1)); + if (t == p) { + p = 0; + continue; + } + p = t; + if (!dte_flags[t]) { +// if ((!dte_flags[t]) && (dte_flags[t2] != 3)) { + if ((dte_counters[t]++) == 0) { + nb_dte++; + } + if (dte_counters[t] > dte_counter) { + dte_most = t; + dte_counter = dte_counters[t]; + } +// } else if (dte_flags[t] == 3) { +// i++; + } + } +} + +void push_entry(long entry) { + int i; + char t[3], c1, c2; + t[2] = 0; + + c1 = dte_most & 0xff; + c2 = dte_most >> 8; + t[0] = things[c1][0]; + t[1] = things[c2][0]; + + for (i = 0; i < 256; i++) { + if (!dte_entries[i]) { + dte_entries[i] = entry; + things[i] = strdup(t); + thingtree::addstring(t, i); + break; + } + } +} + +char * read_line(FILE * f, char * b) { + int r, pos = 0; + + while (!feof(f)) { + if ((r = fgetc(f)) == EOF) + break; + if (r == 0x0d) { + if ((r = fgetc(f)) == EOF) + break; + } + if (r == 0x0a) { + b[pos] = 0; + return b; + } + b[pos++] = r; + } + + b[pos] = 0; + + return b; +} + +void dte_compress() { + int i, j; + char c1, c2; + + for (i = 0; i < 256; i++) { + for (j = 0; j < 256; j++) { + dte_flags[i * 256 + j] = alloweds[i] ? alloweds[j] ? 0 : 1 : 1; + } + } + + gain = 0; + + printm(M_STATUS, "Going for it: dte_size = %i\n", dte_size); + for (i = 0; i < dte_size; i++) { + dte_reset(); + build_dte(); + if (!tnb_dte) + tnb_dte = nb_dte; + c1 = dte_most & 0xff; + c2 = dte_most >> 8; + c1 = things[c1][0]; + c2 = things[c2][0]; + printm(M_INFO, "Entry #%i, most count: %li, couple = 0x%04x = (%c %c)\n", i, dte_counter, (int) dte_most, c1, c2); + dte_flags[dte_most] = 3; + gain += dte_counter; + push_entry(dte_most); + } + + printm(M_STATUS, "Estimated gain: %li bytes, new file size = %li\n", gain, dte_text_size - gain); +} + +void read_thingy(FILE * f) { + char line[256], * st, * thing; + long code; + int i; + + st = line + 2; + thing = line + 5; + line[0] = '0'; + line[1] = 'x'; + + dte_size = 0; + + for (i = 0; i < 256; i++) { + dte_entries[i] = -1; + } + + while (!feof(f)) { + if (strlen(read_line(f, st))) { + line[4] = 0; + sscanf(line, "%li", &code); + switch (strlen(thing)) { + case 0: + dte_size++; + dte_entries[code] = 0; + break; + case 1: + alloweds[code] = 1; + default: + things[code] = strdup(thing); + thingtree::addstring(thing, code); + } + } + } +} + +void read_thingy_file(FILE * f) { + char line[10240], * p, * c, trans[5]; + long code; + int ptr = 0, i; + + for (i = 0; i < 256; i++) { + dte_usage[i] = 0; + } + + while (!feof(f)) { + if (!strlen(p = read_line(f, line))) + continue; + while (*p) { + if (*p == '<') { + if (!(c = strchr(p, '>'))) { + printm(M_ERROR, "Error in file: '<' not closed.\n"); + exit(-1); + } + p++; + if ((c - p) == 2) { + *c = 0; + sprintf(trans, "0x%s", p); + sscanf(trans, "%li", &code); + dte_text[ptr++] = code; + printm(M_BARE, "0x%02x-", code); + } else { + printm(M_BARE, "Unknow %s-", p); + } + p = c + 1; + } else { + if ((code = thingtree::look(p)) == -1) + exit(-1); + dte_text[ptr++] = code; + p += strlen(things[code]); + printm(M_BARE, "%s-", things[code]); + dte_usage[code]++; + } + } + printm(M_BARE, "\n"); + } + + dte_text[ptr] = dte_text[ptr + 1] = dte_text[ptr + 2] = dte_text[ptr + 3] = 0; + dte_text_size = ptr; +} diff --git a/lib/fileutils.cpp b/lib/fileutils.cpp new file mode 100644 index 0000000..b04a414 --- /dev/null +++ b/lib/fileutils.cpp @@ -0,0 +1,74 @@ +/* + * PSX-Tools Bundle Pack + * Copyright (C) 2002 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include "generic.h" + +unsigned long filesize(int f_iso) +{ + long curpos, length; + + curpos = lseek(f_iso, 0, SEEK_CUR); + length = lseek(f_iso, 0, SEEK_END); + lseek(f_iso, curpos, SEEK_SET); + return length; +} + +void copy(int s, int d, long size) { + long i; + unsigned char c; + long r; + + for (i = 0; (i < size) || (size < 0); i++) { + r = read(s, &c, 1); + if (r == 0) { + break; + } + write(d, &c, 1); + } +} + +unsigned long filesize(FILE * f_iso) +{ + long curpos, length; + + curpos = ftell(f_iso); + fseek(f_iso, 0, SEEK_END); + length = ftell(f_iso); + fseek(f_iso, curpos, SEEK_SET); + return length; +} + +void copy(FILE * s, FILE * d, long size) { + long i; + unsigned char c; + long r; + + for (i = 0; (i < size) || (size < 0); i++) { + r = fread(&c, 1, 1, s); + if (r == 0) { + break; + } + fwrite(&c, 1, 1, d); + } +} diff --git a/lib/generic.cpp b/lib/generic.cpp new file mode 100644 index 0000000..d165d27 --- /dev/null +++ b/lib/generic.cpp @@ -0,0 +1,56 @@ +/* + * PSX-Tools Bundle Pack + * Copyright (C) 2002 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include + +char verbosity = 0; + +char * heads[] = {"EE", "--", "WW", "II"}; + +void printm(int level, char * m, ...) { + va_list ap; + + if (verbosity < level) { + return; + } + + if (level >= 0) { + fprintf(stderr, "(%s) ", heads[level]); + } + + va_start(ap, m); + vfprintf(stderr, m, ap); + va_end(ap); +} + +char ** split(char * s, char t) { + static char * p[100]; + int i; + + for (i = 1, p[0] = s; *s; s++) { + if (*s == t) { + *s = 0; + p[i++] = s + 1; + } + } + p[i] = 0; + + return p; +} diff --git a/lib/lzss.cpp b/lib/lzss.cpp new file mode 100644 index 0000000..4f9e424 --- /dev/null +++ b/lib/lzss.cpp @@ -0,0 +1,438 @@ +/* + * PSX-Tools Bundle Pack + * Copyright (C) 2002 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include "fileutils.h" +#include "generic.h" +#include "lzss.h" + +int lzss_maxsize = 18; +int lzss_maxptr = 0x0fff; +int tolerate = 1; +int blockb = 0; + +scheme_t schemes[] = { +/* Nom 1 I J O N 16 P F W Lm1 Ls1 Lm2 Ls2 Jm1 Js1 Jm2 Js2 Fm1 Fs1 Fm2 Fs2 Vm1 Vs1 Vm2 Vs2 */ + {"Xenogears", 1, 0, 0, 1, 0, 0, 0, 0, 0, 0x00, 0, 0xf0, -4, 0xff, 0, 0x0f, 8, 0x00, 0, 0x00, 0, 0x00, 0, 0x00, 0}, + {"DBZ RPG", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x0f, 0, 0x00, 0, 0xf0, -4, 0xff, 4, 0x00, 0, 0x00, 0, 0x00, 0, 0x00, 0}, + {"FF7", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0, 0x0f, 0, 0xff, 0, 0xf0, 4, 0x00, 0, 0x00, 0, 0x00, 0, 0x00, 0}, + {"Leen Mean", 1, 1, 1, 1, 0, 0, 0, 0, 0, 0x0f, 0, 0x00, 0, 0xf0, 4, 0xff, 0, 0x00, 0, 0x00, 0, 0x00, 0, 0x00, 0}, + {"Metal Max", 0, 0, 0, 1, 0, 0, 2, 0, 0x12, 0x00, 0, 0x0f, 0, 0xff, 0, 0xf0, 4, 0x00, 0, 0x00, 0, 0x00, 0, 0x00, 0}, + {"Ogre Battle", 0, 0, 0, 1, 0, 0, 1, 0, 0, 0xf8, -3, 0x00, 0, 0x07, 8, 0xff, 0, 0x00, 0, 0x00, 0, 0x00, 0, 0x00, 0}, + {"Lodoss Wars", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0, 0x7f, 0, 0xff, 0, 0x80, 1, 0x00, 0, 0x00, 0, 0x00, 0, 0x00, 0}, + {"FF6 PSX", 0, 0, 0, 1, 1, 1, 0, 0, 0, 0x1f, 1, 0x00, 0, 0xe0, -4, 0xff, 4, 0x00, 0, 0x00, 0, 0x00, 0, 0x00, 0}, + {"Valkyrie-1", 0, 0, 0, 1, 1, 0, 0, 0, 0, 0x00, 0, 0xf0, -4, 0xff, 0, 0x0f, 8, 0x00, 0, 0x00, 0, 0x00, 0, 0x00, 0}, + {"Valkyrie-2", 0, 0, 0, 1, 1, 0, 0, 2, 0, 0x00, 0, 0xf0, -4, 0xff, 0, 0x0f, 8, 0x00, 0, 0x0f, 0, 0xff, 0, 0x00, 0}, + {0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0, 0x00, 0, 0x00, 0, 0x00, 0} +}; + +scheme_t scheme = schemes[0]; + +char swap_bits(char i) { + i = ((i >> 1) & 0x55) | ((i << 1) & 0xaa); + i = ((i >> 2) & 0x33) | ((i << 2) & 0xcc); + i = ((i >> 4) & 0x0f) | ((i << 4) & 0xf0); + return i; +} + +unsigned int shift(unsigned int c, int s) { + return s > 0 ? (c << s) : c >> (-s); +} + +void compute_limits(void) { + unsigned char val1, val2; + + val1 = val2 = 0xff; + + lzss_maxsize = shift(val1 & scheme.l_mask_1, scheme.l_shft_1) | + shift(val2 & scheme.l_mask_2, scheme.l_shft_2); + lzss_maxptr = shift(val1 & scheme.j_mask_1, scheme.j_shft_1) | + shift(val2 & scheme.j_mask_2, scheme.j_shft_2); + + lzss_maxsize = lzss_maxsize + 3 + scheme.sixteen_bits; + lzss_maxptr += scheme.one_jump; + + if (scheme.l_mask_1 & scheme.j_mask_1) { + printm(M_ERROR, "Masks are overlapping for value 1\n"); + exit(-1); + } + + if (scheme.l_mask_2 & scheme.j_mask_2) { + printm(M_ERROR, "Masks are overlapping for value 2\n"); + exit(-1); + } + + if (shift(scheme.l_mask_1, scheme.l_shft_1) & shift(scheme.l_mask_2, scheme.l_shft_2)) { + printm(M_ERROR, "Shifts build an overlap for lenght\n"); + exit(-1); + } + + if (shift(scheme.j_mask_1, scheme.j_shft_1) & shift(scheme.j_mask_2, scheme.j_shft_2)) { + printm(M_ERROR, "Shifts build an overlap for jump\n"); + exit(-1); + } + + printm(M_INFO, "Computed values: maxsize = %i, maxptr = 0x%06x\n", lzss_maxsize, lzss_maxptr); +} + +unsigned long lzss_decomp(FILE * f_source, FILE * f_cible, long true_length) +{ + unsigned char bitmap, fbitmap; + unsigned char valeur; + unsigned char * text_buf; + unsigned char val1, val2, val3; + int negative_error = scheme.negative_trick, overlap_error = scheme.overlap_trick; + int r = 0; + int decomp_count; + int decomp_length; + int decomp_fill; + int decomp_jump; + int decomp_offset = 0; + int loop_length; + int whole_count; + int i, j; + long length, reads; + + compute_limits(); + + fread(&length, 1, 4, f_source); + if (true_length >= 0) { + length = true_length; + } + whole_count = 0; + + printm(M_INFO, "Decompressing %i bytes\n", length); + + text_buf = (unsigned char *) malloc(length + 8); + + do { + fread(&bitmap, 1, 1, f_source); + if (scheme.sixteen_bits) { + fread(&fbitmap, 1, 1, f_source); + printm(M_INFO, "16bits behavior, false bitmap = %02x\n", fbitmap); + } + if (scheme.bitmap_inversed) { + bitmap = swap_bits(bitmap); + } + printm(M_INFO, "Begin of block, bitmap = %02x\n", bitmap); + for (i = 0; i < 8; i++) { + printm(M_INFO, " - Chunk %i (offset cible = %li = 0x%04x, offset source = %li = 0x%04x)\n", i, ftell(f_cible), ftell(f_cible), ftell(f_source), ftell(f_source)); + if (whole_count >= length) + break; + if ((bitmap & 1) ^ scheme.one_is_compressed) { + for (j = 0; j < (scheme.sixteen_bits ? 2 : 1); j++) { + reads = fread(&valeur, 1, 1, f_source); + if (!reads) { + printm(M_WARNING, " WARNING! PADDING!\n"); + free(text_buf); + return length; + } + printm(M_INFO, " Copying 0x%02x\n", valeur); + fwrite(&valeur, 1, 1, f_cible); + text_buf[r++] = valeur; + whole_count++; + } + } else { + fread(&val1, 1, 1, f_source); + fread(&val2, 1, 1, f_source); + decomp_length = shift(val1 & scheme.l_mask_1, scheme.l_shft_1) | + shift(val2 & scheme.l_mask_2, scheme.l_shft_2); + decomp_fill = shift(val1 & scheme.f_mask_1, scheme.f_shft_1) | + shift(val2 & scheme.f_mask_2, scheme.f_shft_2); + decomp_jump = shift(val1 & scheme.j_mask_1, scheme.j_shft_1) | + shift(val2 & scheme.j_mask_2, scheme.j_shft_2); + valeur = shift(val1 & scheme.v_mask_1, scheme.v_shft_1) | + shift(val2 & scheme.v_mask_2, scheme.v_shft_2); + decomp_jump &= lzss_maxptr; + decomp_jump += scheme.one_jump; + decomp_length = decomp_length + 3 + scheme.sixteen_bits; + decomp_fill = decomp_fill + 3 + scheme.sixteen_bits; + if ((decomp_length == lzss_maxsize) && (scheme.filling)) { + if ((decomp_fill == 3) && (scheme.filling == 2)) { + fread(&val3, 1, 1, f_source); + printm(M_INFO, " Found an extended needle (val1 = 0x%02x, val2 = 0x%02x, val3 = 0x%02x)\n", val1, val2, val3); + decomp_fill = val1 + 19; + valeur = val3; + } else { + printm(M_INFO, " Found a 0x%02x-filling needle of %li bytes (val1 = 0x%02x, val2 = 0x%02x)\n", valeur, decomp_fill, val1, val2); + } + for (decomp_count = 0; decomp_count < decomp_fill; decomp_count++) { + fwrite(&valeur, 1, 1, f_cible); + text_buf[r++] = valeur; + if (!blockb) + whole_count++; + } + if (blockb) + whole_count++; + } else { + switch (scheme.ptrb) { + case 0: + decomp_offset = r - decomp_jump; + break; + case 1: + decomp_offset = r - lzss_maxptr - 1 + decomp_jump; + break; + case 2: + decomp_offset = decomp_jump - scheme.window_start; + break; + } + loop_length = decomp_offset + decomp_length; + if ((loop_length >= r) && (!overlap_error)) { + if (!tolerate) { + free(text_buf); + return 0; + } + printm(M_ERROR, "Overlap trick used without it beeing enabled in the scheme.\n"); + overlap_error = 1; + } + printm(M_INFO, " Found a needle of %li bytes at %li = 0x%04x, jump of %li = 0x%04x (val1 = 0x%02x, val2 = 0x%02x)\n", decomp_length, decomp_offset, decomp_offset, decomp_jump, decomp_jump, val1, val2); + for (decomp_count = decomp_offset; decomp_count < loop_length; decomp_count++) { + if (!blockb) + whole_count++; + if (decomp_count < 0) { + valeur = 0; + fwrite(&valeur, 1, 1, f_cible); + text_buf[r++] = 0; + if (!negative_error) { + if (!tolerate) { + free(text_buf); + return 0; + } + printm(M_ERROR, "Negative trick used without it beeing enabled in the scheme.\n"); + negative_error = 1; + } + printm(M_INFO, "Filling with 0\n"); + } else { + fwrite(&text_buf[decomp_count], 1, 1, f_cible); + printm(M_INFO, "@0x%04x: 0x%02x\n", decomp_count, text_buf[decomp_count]); + text_buf[r++] = text_buf[decomp_count]; + } + if (whole_count >= length) + break; + } + if (blockb) + whole_count++; + } + } + bitmap >>= 1; + } + } while (whole_count < length); + free(text_buf); + + return length; +} + +unsigned char lzss_rd(unsigned char * t, long p) { + return ((p < 0) ? 0 : (t[p])); +} + +long lzss_comp_strstr(unsigned char * needle, unsigned char * r, long * l, long sp) { + char redo[256]; + long length, i, p, ptr, maxlength; + + i = 1; + redo[0] = p = 0; + while (i < lzss_maxsize) { + if (needle[i] == needle[p]) { + redo[i++] = ++p; + } else if (p > 0) { + p = redo[p - 1]; + } else { + redo[i++] = 0; + } + } + + length = maxlength = 0; + i = sp; + p = 0; + ptr = 0; + + while ((i - sp - (scheme.overlap_trick ? p : 0)) < *l) { + if (needle[p] == lzss_rd(r, i)) { + if (p == (lzss_maxsize - 1)) { + *l = lzss_maxsize; + return i - lzss_maxsize + 1; + } + i++; + p++; + } else if (p > 0) { + if (p > maxlength) { + if (!((i - p) & scheme.sixteen_bits)) { + ptr = i - (maxlength = p); + } + } + p = redo[p - 1]; + } else { + i++; + } + } + + *l = maxlength; + return ptr; +} + +long blk, bitmap_count; + +unsigned char * lzss_memcomp(unsigned char * r, long * l, long * delta) { + unsigned char bitmap, * comp; + long ptr, needle, needle_length, comp_ptr, bitmap_ptr, val1, val2; + long jump, farest, remaining; + int j; + + comp = (unsigned char *) malloc(3 * *l); + + compute_limits(); + + ptr = 0; + blk = 0; + bitmap_count = 0; + comp_ptr = 1 + scheme.sixteen_bits; + bitmap = 0; + bitmap_ptr = 0; + printm(M_INFO, "Begin of block 0.\n"); + while ((remaining = *l - ptr) > 0) { + printm(M_INFO, " Remaining bytes: %li\n", remaining); + bitmap_count++; + bitmap >>= 1; + farest = ptr - lzss_maxptr; + farest = farest > ((-lzss_maxsize) * scheme.negative_trick) ? farest : -lzss_maxsize * scheme.negative_trick; + needle_length = ptr - farest; + if (scheme.ptrb == 2) { + farest = 0; + needle_length = MIN(lzss_maxptr - scheme.window_start, ptr); + } + needle = lzss_comp_strstr(&r[ptr], r, &needle_length, farest); + if ((needle < 0) && ((-needle) > needle_length)) { + needle = -needle_length; + } + printm(M_INFO, " - Chunk %i (offset source = %li = 0x%04x, offset cible = %li = 0x%04x)\n", bitmap_count - 1, ptr, ptr, comp_ptr, comp_ptr); + jump = ptr - needle; + needle_length = needle_length > remaining ? remaining : needle_length; + + if (needle_length & scheme.sixteen_bits) { + needle_length--; + } + + if ((needle < 0) || (!jump)) { + printm(M_INFO, " Nothing found.\n"); + } else { + printm(M_INFO, " Found a needle of %i bytes at offset %i (jump = %i = 0x%04x)\n", needle_length, needle, jump, jump); + } + + if ((needle_length <= (2 + scheme.sixteen_bits)) || (!jump)) { + if (needle_length > 2) { + printm(M_ERROR, " ** REJECTED **\n"); + } + for (j = 0; j < (scheme.sixteen_bits ? 2 : 1); j++) { + printm(M_INFO, " Repeating 0x%02x\n", r[ptr]); + comp[comp_ptr] = r[ptr]; + ptr++; + comp_ptr++; + } + bitmap |= 0x80; + } else { + int j; + printm(M_INFO, " Found a needle of %li bytes at %li = 0x%04x\n", needle_length, needle, needle); + for (j = 0; j < needle_length; j++) { + printm(M_INFO, "@0x%04x: 0x%02x - @0x%04x: 0x%02x\n", needle + j, lzss_rd(r, needle + j - scheme.window_start), ptr + j, lzss_rd(r, ptr + j)); + if (lzss_rd(r, needle + j) != lzss_rd(r, ptr + j)) { + printm(M_ERROR, "ERROR!!\n"); + } + } + jump -= scheme.one_jump; + printm(M_INFO, "ptr = %li, needle = %li, jump = %li = 0x%03x\n", ptr, needle, jump, jump); + ptr += needle_length; + needle_length -= 3; + switch (scheme.ptrb) { + case 0: + break; + case 1: + jump = lzss_maxptr + 1 - jump; + break; + case 2: + jump = needle + scheme.window_start; + break; + } + val1 = comp[comp_ptr++] = (shift(jump, -scheme.j_shft_1) & scheme.j_mask_1) | + (shift(needle_length, -scheme.l_shft_1) & scheme.l_mask_1); + val2 = comp[comp_ptr++] = (shift(jump, -scheme.j_shft_2) & scheme.j_mask_2) | + (shift(needle_length, -scheme.l_shft_2) & scheme.l_mask_2); + printm(M_INFO, " writing info1 = 0x%02x, info2 = 0x%02x\n", val1, val2); + } + + bitmap ^= scheme.one_is_compressed << 7; + + if (bitmap_count == 8) { + blk++; + printm(M_INFO, "End of block, writing bitmap = 0x%02x\n", bitmap); + printm(M_INFO, "Begin of block %li.\n", blk); + bitmap_count = 0; + if (scheme.bitmap_inversed) + bitmap = swap_bits(bitmap); + comp[bitmap_ptr] = bitmap; + if (scheme.sixteen_bits) { + comp[bitmap_ptr + 1] = 0; + } + bitmap_ptr = comp_ptr; + comp_ptr += (scheme.sixteen_bits ? 2 : 1); + } + } + + if (bitmap_count) { + bitmap >>= (8 - bitmap_count); + if (scheme.bitmap_inversed) + bitmap = swap_bits(bitmap); + comp[bitmap_ptr] = bitmap; + if (scheme.sixteen_bits) { + comp[bitmap_ptr + 1] = 0; + } + } else { + comp_ptr--; + } + + if (delta) { + *delta = (bitmap_count ? 8 - bitmap_count : 0); + } + + *l = comp_ptr; + return comp; +} + +void lzss_comp(FILE * f_source, FILE * f_cible, long * delta) { + long length = filesize(f_source), l; + unsigned char * r = (unsigned char *) malloc(length), * c; + + fread(r, 1, length, f_source); + l = length; + c = lzss_memcomp(r, &l, delta); + if (delta) { + length += *delta; + } + fwrite(&length, 1, 4, f_cible); + if (delta) { + length -= *delta; + } + fwrite(c, 1, l, f_cible); + free(c); + free(r); +} diff --git a/lib/yazedc.cpp b/lib/yazedc.cpp new file mode 100644 index 0000000..f4bced0 --- /dev/null +++ b/lib/yazedc.cpp @@ -0,0 +1,858 @@ +/* + * PSX-Tools Bundle Pack + * Copyright (C) 1998 Heiko Eissfeldt + * portions used& Chris Smith + * Modified by Yazoo, then by + * Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include + +#define RS_L12_BITS 8 + +/* audio sector definitions for CIRC */ +#define FRAMES_PER_SECTOR 98 +/* user data bytes per frame */ +#define L1_RAW 24 +/* parity bytes with 8 bit */ +#define L1_Q 4 +#define L1_P 4 + +/* audio sector Cross Interleaved Reed-Solomon Code (CIRC) encoder (layer 1) */ +/* adds P- and Q- parity information to audio (f2) frames. Also + optionally handles the various delays and permutations. The output with all + stages enabled can be fed into the Eight-Fourteen-Modulator. + On input: 2352 bytes of audio data is given. + On output: 3136 bytes of CIRC enriched audio data are returned. + */ +int do_encode_L1(unsigned char in[L1_RAW*FRAMES_PER_SECTOR], + unsigned char out[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR], + int delay1, int delay2, int delay3, int scramble); + +/* data sector definitions for RSPC */ +/* user data bytes per frame */ +#define L2_RAW (1024*2) +/* parity bytes for 16 bit units */ +#define L2_Q (26*2*2) +#define L2_P (43*2*2) + +/* known sector types */ +#define MODE_0 0 +#define MODE_1 1 +#define MODE_2 2 +#define MODE_2_FORM_1 3 +#define MODE_2_FORM_2 4 + +#ifdef __cplusplus +extern "C" { +#endif + +/* set one of the MODE_* constants for subsequent data sector formatting */ +int set_sector_type(int st); +/* get the current sector type setting for data sector formatting */ +int get_sector_type(void); + +/* data sector layer 2 Reed-Solomon Product Code encoder */ +/* encode the given data portion depending on sector type (see + get/set_sector_type() functions). Use the given address for the header. + The returned data is __unscrambled__ and not in F2-frame format (for that + see function scramble_L2()). + Supported sector types: + MODE_0: a 12-byte sync field, a header and 2336 zeros are returned. + MODE_1: the user data portion (2048 bytes) has to be given + at offset 16 in the inout array. + Sync-, header-, edc-, spare-, p- and q- fields will be added. + MODE_2: the user data portion (2336 bytes) has to be given + at offset 16 in the inout array. + Sync- and header- fields will be added. + MODE_2_FORM_1: the user data portion (8 bytes subheader followed + by 2048 bytes data) has to be given at offset 16 + in the inout array. + Sync-, header-, edc-, p- and q- fields will be added. + MODE_2_FORM_2: the user data portion (8 bytes subheader followed + by 2324 bytes data) has to be given at offset 16 + in the inout array. + Sync-, header- and edc- fields will be added. +*/ +int do_encode_L2(unsigned char *inout, int sectortype, unsigned address); +int decode_L2_Q(unsigned char inout[4 + L2_RAW + 12 + L2_Q]); +int decode_L2_P(unsigned char inout[4 + L2_RAW + 12 + L2_Q + L2_P]); +unsigned long int build_edc(unsigned char inout[], int from, int upto); + +/* generates f2 frames from otherwise fully formatted sectors (generated by + do_encode_L2()). */ +int scramble_L2(unsigned char *inout); + +#ifdef __cplusplus +} +#endif + +/* r-w sub channel definitions */ +#define RS_SUB_RW_BITS 6 + +#define PACKETS_PER_SUBCHANNELFRAME 4 +#define LSUB_RAW 18 +#define LSUB_QRAW 2 +/* 6 bit */ +#define LSUB_Q 2 +#define LSUB_P 4 + +#ifdef __cplusplus +extern "C" { +#endif + +/* R-W subchannel encoder */ +/* On input: 72 bytes packed user data, four frames with each 18 bytes. + On output: per frame: 2 bytes user data, 2 bytes Q parity, + 16 bytes user data, 4 bytes P parity. + Options: + delay1: use low level delay line + scramble: perform low level permutations + */ +int do_encode_sub(unsigned char in[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME], + unsigned char out[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME], + int delay1, int scramble); +int do_decode_sub(unsigned char in[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME], + unsigned char out[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME], + int delay1, int scramble); + +int decode_LSUB_Q(unsigned char inout[LSUB_QRAW + LSUB_Q]); +int decode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P]); + +#ifdef __cplusplus +} +#endif + +unsigned char minute, second, frame; + +/* these prototypes will become public when the function are implemented */ +#ifdef MAIN +static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)], + unsigned char out[L2_RAW]); +static int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR], + unsigned char out[L1_RAW*FRAMES_PER_SECTOR], + int delay1, int delay2, int delay3, int scramble); +#endif + +/* ------------- tables generated by gen_encodes --------------*/ + +#include "crctables" + +static int encode_L1_Q(unsigned char inout[L1_RAW + L1_Q]) +{ + unsigned char *Q; + int i; + + memmove(inout+L1_RAW/2+L1_Q, inout+L1_RAW/2, L1_RAW/2); + Q = inout + L1_RAW/2; + + memset(Q, 0, L1_Q); + for (i = 0; i < L1_RAW + L1_Q; i++) { + unsigned char data; + + if (i == L1_RAW/2) i += L1_Q; + data = inout[i]; + if (data != 0) { + unsigned char base = rs_l12_log[data]; + + Q[0] ^= rs_l12_alog[(base+AQ[0][i]) % ((1 << RS_L12_BITS)-1)]; + Q[1] ^= rs_l12_alog[(base+AQ[1][i]) % ((1 << RS_L12_BITS)-1)]; + Q[2] ^= rs_l12_alog[(base+AQ[2][i]) % ((1 << RS_L12_BITS)-1)]; + Q[3] ^= rs_l12_alog[(base+AQ[3][i]) % ((1 << RS_L12_BITS)-1)]; + } + } + + return 0; +} +static int encode_L1_P(unsigned char inout[L1_RAW + L1_Q + L1_P]) +{ + unsigned char *P; + int i; + + P = inout + L1_RAW + L1_Q; + + memset(P, 0, L1_P); + for (i = 0; i < L2_RAW + L2_Q + L2_P; i++) { + unsigned char data; + + data = inout[i]; + if (data != 0) { + unsigned char base = rs_l12_log[data]; + + P[0] ^= rs_l12_alog[(base+AP[0][i]) % ((1 << RS_L12_BITS)-1)]; + P[1] ^= rs_l12_alog[(base+AP[1][i]) % ((1 << RS_L12_BITS)-1)]; + P[2] ^= rs_l12_alog[(base+AP[2][i]) % ((1 << RS_L12_BITS)-1)]; + P[3] ^= rs_l12_alog[(base+AP[3][i]) % ((1 << RS_L12_BITS)-1)]; + } + } + return 0; +} + +#ifdef MAIN +static int decode_L1_Q(unsigned char inout[L1_RAW + L1_Q]) +{ + return 0; +} + +static int decode_L1_P(unsigned char in[L1_RAW + L1_Q + L1_P]) +{ + return 0; +} +#endif + +static int encode_L2_Q(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q]) +{ + unsigned char *Q; + int i,j; + + Q = inout + 4 + L2_RAW + 4 + 8 + L2_P; + memset(Q, 0, L2_Q); + for (j = 0; j < 26; j++) { + for (i = 0; i < 43; i++) { + unsigned char data; + + /* LSB */ + data = inout[(j*43*2+i*2*44) % (4 + L2_RAW + 4 + 8 + L2_P)]; + if (data != 0) { + unsigned int base = rs_l12_log[data]; + + unsigned int sum = base + DQ[0][i]; + if (sum >= ((1 << RS_L12_BITS)-1)) + sum -= (1 << RS_L12_BITS)-1; + + Q[0] ^= rs_l12_alog[sum]; + + sum = base + DQ[1][i]; + if (sum >= ((1 << RS_L12_BITS)-1)) + sum -= (1 << RS_L12_BITS)-1; + + Q[26*2] ^= rs_l12_alog[sum]; + } + /* MSB */ + data = inout[(j*43*2+i*2*44+1) % (4 + L2_RAW + 4 + 8 + L2_P)]; + if (data != 0) { + unsigned int base = rs_l12_log[data]; + + unsigned int sum = base+DQ[0][i]; + if (sum >= ((1 << RS_L12_BITS)-1)) + sum -= (1 << RS_L12_BITS)-1; + + Q[1] ^= rs_l12_alog[sum]; + + sum = base + DQ[1][i]; + if (sum >= ((1 << RS_L12_BITS)-1)) + sum -= (1 << RS_L12_BITS)-1; + + Q[26*2+1] ^= rs_l12_alog[sum]; + } + } + Q += 2; + } + return 0; +} + +static int encode_L2_P(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P]) +{ + unsigned char *P; + int i,j; + + P = inout + 4 + L2_RAW + 4 + 8; + memset(P, 0, L2_P); + for (j = 0; j < 43; j++) { + for (i = 0; i < 24; i++) { + unsigned char data; + + /* LSB */ + data = inout[i*2*43]; + if (data != 0) { + unsigned int base = rs_l12_log[data]; + + unsigned int sum = base + DP[0][i]; + if (sum >= ((1 << RS_L12_BITS)-1)) + sum -= (1 << RS_L12_BITS)-1; + + P[0] ^= rs_l12_alog[sum]; + + sum = base + DP[1][i]; + if (sum >= ((1 << RS_L12_BITS)-1)) + sum -= (1 << RS_L12_BITS)-1; + + P[43*2] ^= rs_l12_alog[sum]; + } + /* MSB */ + data = inout[i*2*43+1]; + if (data != 0) { + unsigned int base = rs_l12_log[data]; + + unsigned int sum = base + DP[0][i]; + if (sum >= ((1 << RS_L12_BITS)-1)) + sum -= (1 << RS_L12_BITS)-1; + + P[1] ^= rs_l12_alog[sum]; + + sum = base + DP[1][i]; + if (sum >= ((1 << RS_L12_BITS)-1)) + sum -= (1 << RS_L12_BITS)-1; + + P[43*2+1] ^= rs_l12_alog[sum]; + } + } + P += 2; + inout += 2; + } + return 0; +} + +int decode_L2_Q(unsigned char inout[4 + L2_RAW + 12 + L2_Q]) +{ + return 0; +} +int decode_L2_P(unsigned char inout[4 + L2_RAW + 12 + L2_Q + L2_P]) +{ + return 0; +} + +int scramble_L2(unsigned char *inout) +{ + unsigned char *r = inout + 12; + unsigned char *s = yellowbook_scrambler; + unsigned int i; + unsigned int *f = (unsigned int *)inout; + + for (i = (L2_RAW + L2_Q + L2_P +16)/sizeof(unsigned char); i; i--) { + *r++ ^= *s++; + } + + /* generate F1 frames */ + for (i = (2352/sizeof(unsigned int)); i; i--) { + *f++ = ((*f & 0xff00ff00UL) >> 8) | ((*f & 0x00ff00ffUL) << 8); + } + + return 0; +} + +static int encode_LSUB_Q(unsigned char inout[LSUB_RAW + LSUB_Q]) +{ + unsigned char *Q; +/* unsigned char data; */ + int i; + + memmove(inout+LSUB_QRAW+LSUB_Q, inout+LSUB_QRAW, LSUB_RAW-LSUB_QRAW); + Q = inout + LSUB_QRAW; + + memset(Q, 0, LSUB_Q); + +#if 0 + data = inout[0] & 0x3f; + if (data != 0) { + unsigned char base = rs_sub_rw_log[data]; + + Q[0] ^= rs_sub_rw_alog[(base+26) % ((1 << RS_SUB_RW_BITS)-1)]; + Q[1] ^= rs_sub_rw_alog[(base+7) % ((1 << RS_SUB_RW_BITS)-1)]; + } + data = inout[1] & 0x3f; + if (data != 0) { + unsigned char base = rs_sub_rw_log[data]; + + Q[0] ^= rs_sub_rw_alog[(base+6) % ((1 << RS_SUB_RW_BITS)-1)]; + Q[1] ^= rs_sub_rw_alog[(base+1) % ((1 << RS_SUB_RW_BITS)-1)]; + } +#else + for (i = 0; i < LSUB_QRAW; i++) { + unsigned char data; + + data = inout[i] & 0x3f; + if (data != 0) { + unsigned char base = rs_sub_rw_log[data]; + + Q[0] ^= rs_sub_rw_alog[(base+SQ[0][i]) % ((1 << RS_SUB_RW_BITS)-1)]; + Q[1] ^= rs_sub_rw_alog[(base+SQ[1][i]) % ((1 << RS_SUB_RW_BITS)-1)]; + } + } +#endif + return 0; +} + + +static int encode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P]) +{ + unsigned char *P; + int i; + + P = inout + LSUB_RAW + LSUB_Q; + + memset(P, 0, LSUB_P); + for (i = 0; i < LSUB_RAW + LSUB_Q; i++) { + unsigned char data; + + data = inout[i] & 0x3f; + if (data != 0) { + unsigned char base = rs_sub_rw_log[data]; + + P[0] ^= rs_sub_rw_alog[(base+SP[0][i]) % ((1 << RS_SUB_RW_BITS)-1)]; + P[1] ^= rs_sub_rw_alog[(base+SP[1][i]) % ((1 << RS_SUB_RW_BITS)-1)]; + P[2] ^= rs_sub_rw_alog[(base+SP[2][i]) % ((1 << RS_SUB_RW_BITS)-1)]; + P[3] ^= rs_sub_rw_alog[(base+SP[3][i]) % ((1 << RS_SUB_RW_BITS)-1)]; + } + } + return 0; +} + +int decode_LSUB_Q(unsigned char inout[LSUB_QRAW + LSUB_Q]) +{ + unsigned char Q[LSUB_Q]; + int i; + + memset(Q, 0, LSUB_Q); + for (i = LSUB_QRAW + LSUB_Q -1; i>=0; i--) { + unsigned char data; + + data = inout[LSUB_QRAW + LSUB_Q -1 -i] & 0x3f; + if (data != 0) { + unsigned char base = rs_sub_rw_log[data]; + + Q[0] ^= rs_sub_rw_alog[(base+0*i) % ((1 << RS_SUB_RW_BITS)-1)]; + Q[1] ^= rs_sub_rw_alog[(base+1*i) % ((1 << RS_SUB_RW_BITS)-1)]; + } + } + + return (Q[0] != 0 || Q[1] != 0); +} +int decode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P]) +{ + unsigned char P[LSUB_P]; + int i; + + memset(P, 0, LSUB_P); + for (i = LSUB_RAW + LSUB_Q + LSUB_P-1; i>=0; i--) { + unsigned char data; + + data = inout[LSUB_RAW + LSUB_Q + LSUB_P -1 -i] & 0x3f; + if (data != 0) { + unsigned char base = rs_sub_rw_log[data]; + + P[0] ^= rs_sub_rw_alog[(base+0*i) % ((1 << RS_SUB_RW_BITS)-1)]; + P[1] ^= rs_sub_rw_alog[(base+1*i) % ((1 << RS_SUB_RW_BITS)-1)]; + P[2] ^= rs_sub_rw_alog[(base+2*i) % ((1 << RS_SUB_RW_BITS)-1)]; + P[3] ^= rs_sub_rw_alog[(base+3*i) % ((1 << RS_SUB_RW_BITS)-1)]; + } + } + + return (P[0] != 0 || P[1] != 0 || P[2] != 0 || P[3] != 0); +} + +/* Layer 1 CIRC en/decoder */ +#define MAX_L1_DEL1 2 +static unsigned char l1_delay_line1[MAX_L1_DEL1][L1_RAW]; +#define MAX_L1_DEL2 108 +static unsigned char l1_delay_line2[MAX_L1_DEL2][L1_RAW+L1_Q]; +#define MAX_L1_DEL3 1 +static unsigned char l1_delay_line3[MAX_L1_DEL3][L1_RAW+L1_Q+L1_P]; +static unsigned l1_del_index; + +int do_encode_L1(unsigned char in[L1_RAW*FRAMES_PER_SECTOR], + unsigned char out[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR], + int delay1, int delay2, int delay3, int permute) +{ + int i; + + for (i = 0; i < FRAMES_PER_SECTOR; i++) { + int j; + unsigned char t; + + if (in != out) + memcpy(out, in, L1_RAW); + + if (delay1) { + /* shift through delay line 1 */ + for (j = 0; j < L1_RAW; j++) { + if (((j/4) % MAX_L1_DEL1) == 0) { + t = l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j]; + l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j] = out[j]; + out[j] = t; + } + } + } + + if (permute) { + /* permute */ + t = out[2]; out[2] = out[8]; out[8] = out[10]; out[10] = out[18]; + out[18] = out[6]; out [6] = t; + t = out[3]; out[3] = out[9]; out[9] = out[11]; out[11] = out[19]; + out[19] = out[7]; out [7] = t; + t = out[4]; out[4] = out[16]; out[16] = out[20]; out[20] = out[14]; + out[14] = out[12]; out [12] = t; + t = out[5]; out[5] = out[17]; out[17] = out[21]; out[21] = out[15]; + out[15] = out[13]; out [13] = t; + } + + /* build Q parity */ + encode_L1_Q(out); + + if (delay2) { + /* shift through delay line 2 */ + for (j = 0; j < L1_RAW+L1_Q; j++) { + if (j != 0) { + t = l1_delay_line2[(l1_del_index) % MAX_L1_DEL2][j]; + l1_delay_line2[(l1_del_index + j*4) % MAX_L1_DEL2][j] = out[j]; + out[j] = t; + } + } + } + + /* build P parity */ + encode_L1_P(out); + + if (delay3) { + /* shift through delay line 3 */ + for (j = 0; j < L1_RAW+L1_Q+L1_P; j++) { + if (((j) & MAX_L1_DEL3) == 0) { + t = l1_delay_line3[0][j]; + l1_delay_line3[0][j] = out[j]; + out[j] = t; + } + } + } + + /* invert Q and P parity */ + for (j = 0; j < L1_Q; j++) + out[j+12] = ~out[j+12]; + for (j = 0; j < L1_P; j++) + out[j+28] = ~out[j+28]; + + l1_del_index++; + out += L1_RAW+L1_Q+L1_P; + in += L1_RAW; + } + return 0; +} + +#ifdef MAIN +int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR], + unsigned char out[L1_RAW*FRAMES_PER_SECTOR], + int delay1, int delay2, int delay3, int permute) +{ + int i; + + for (i = 0; i < FRAMES_PER_SECTOR; i++) { + int j; + unsigned char t; + + if (delay3) { + /* shift through delay line 3 */ + for (j = 0; j < L1_RAW+L1_Q+L1_P; j++) { + if (((j) & MAX_L1_DEL3) != 0) { + t = l1_delay_line3[0][j]; + l1_delay_line3[0][j] = in[j]; + in[j] = t; + } + } + } + + /* invert Q and P parity */ + for (j = 0; j < L1_Q; j++) + in[j+12] = ~in[j+12]; + for (j = 0; j < L1_P; j++) + in[j+28] = ~in[j+28]; + + /* build P parity */ + decode_L1_P(in); + + if (delay2) { + /* shift through delay line 2 */ + for (j = 0; j < L1_RAW+L1_Q; j++) { + if (j != L1_RAW+L1_Q-1) { + t = l1_delay_line2[(l1_del_index) % MAX_L1_DEL2][j]; + l1_delay_line2[(l1_del_index + (MAX_L1_DEL2 - j*4)) % MAX_L1_DEL2][j] = in[j]; + in[j] = t; + } + } + } + + /* build Q parity */ + decode_L1_Q(in); + + if (permute) { + /* permute */ + t = in[2]; in[2] = in[6]; in[6] = in[18]; in[18] = in[10]; + in[10] = in[8]; in [8] = t; + t = in[3]; in[3] = in[7]; in[7] = in[19]; in[19] = in[11]; + in[11] = in[9]; in [9] = t; + t = in[4]; in[4] = in[12]; in[12] = in[14]; in[14] = in[20]; + in[20] = in[16]; in [16] = t; + t = in[5]; in[5] = in[13]; in[13] = in[15]; in[15] = in[21]; + in[21] = in[17]; in [17] = t; + } + + if (delay1) { + /* shift through delay line 1 */ + for (j = 0; j < L1_RAW; j++) { + if (((j/4) % MAX_L1_DEL1) != 0) { + t = l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j]; + l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j] = in[j]; + in[j] = t; + } + } + } + + if (in != out) + memcpy(out, in, (L1_RAW)); + + l1_del_index++; + in += L1_RAW+L1_Q+L1_P; + out += L1_RAW; + } + return 0; +} + +#endif + +#if 0 +static unsigned char bin2bcd(unsigned p) +{ + return ((p/10)<<4)|(p%10); +} +#endif + +static int build_address(unsigned char inout[], int sectortype, unsigned address) +{ + inout[12] = minute; + inout[13] = second; + inout[14] = frame; + if (sectortype == MODE_0) + inout[15] = 0; + else if (sectortype == MODE_1) + inout[15] = 1; + else if (sectortype == MODE_2) + inout[15] = 2; + else if (sectortype == MODE_2_FORM_1) + inout[15] = 2; + else if (sectortype == MODE_2_FORM_2) + inout[15] = 2; + else + return -1; + return 0; +} + +#include "crctable.out" + +unsigned long int build_edc(unsigned char inout[], int from, int upto) +{ + unsigned char *p = inout+from; + unsigned long result = 0; + + for (; from <= upto; from++) + result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8); + + return result; +} + +/* Layer 2 Product code en/decoder */ +int do_encode_L2(unsigned char inout[(12 + 4 + L2_RAW+4+8+L2_Q+L2_P)], int sectortype, unsigned address) +{ + unsigned long int result; + +#define SYNCPATTERN "\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + + /* supply initial sync pattern */ + memcpy(inout, SYNCPATTERN, sizeof(SYNCPATTERN)); + + if (sectortype == MODE_0) { + memset(inout + sizeof(SYNCPATTERN), 0, 4 + L2_RAW + 12 + L2_P + L2_Q); + build_address(inout, sectortype, address); + return 0; + } + + switch (sectortype) { + case MODE_1: + build_address(inout, sectortype, address); + result = build_edc(inout, 0, 16+2048-1); + inout[2064+0] = result >> 0L; + inout[2064+1] = result >> 8L; + inout[2064+2] = result >> 16L; + inout[2064+3] = result >> 24L; + memset(inout+2064+4, 0, 8); + encode_L2_P(inout+12); + encode_L2_Q(inout+12); + break; + case MODE_2: + build_address(inout, sectortype, address); + break; + case MODE_2_FORM_1: + result = build_edc(inout, 16, 16+8+2048-1); + inout[2072+0] = result >> 0L; + inout[2072+1] = result >> 8L; + inout[2072+2] = result >> 16L; + inout[2072+3] = result >> 24L; + + /* clear header for P/Q parity calculation */ + inout[12] = 0; + inout[12+1] = 0; + inout[12+2] = 0; + inout[12+3] = 0; + encode_L2_P(inout+12); + encode_L2_Q(inout+12); + build_address(inout, sectortype, address); + break; + case MODE_2_FORM_2: + build_address(inout, sectortype, address); + result = build_edc(inout, 16, 16+8+2324-1); + inout[2348+0] = result >> 0L; + inout[2348+1] = result >> 8L; + inout[2348+2] = result >> 16L; + inout[2348+3] = result >> 24L; + break; + default: + return -1; + } + + return 0; +} + +#ifdef MAIN +static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)], + unsigned char out[L2_RAW]) +{ + return 0; +} +#endif + + +#define MAX_SUB_DEL 8 +static unsigned char sub_delay_line[MAX_SUB_DEL][LSUB_RAW+LSUB_Q+LSUB_P]; +static unsigned sub_del_index; + +/* R-W Subchannel en/decoder */ +int do_encode_sub(unsigned char in[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME], + unsigned char out[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME], + int delay1, int permute) +{ + int i; + + if (in == out) return -1; + + for (i = 0; i < PACKETS_PER_SUBCHANNELFRAME; i++) { + int j; + unsigned char t; + + memcpy(out, in, (LSUB_RAW)); + + /* build Q parity */ + encode_LSUB_Q(out); + + /* build P parity */ + encode_LSUB_P(out); + + if (permute) { + /* permute */ + t = out[1]; out[1] = out[18]; out[18] = t; + t = out[2]; out[2] = out[ 5]; out[ 5] = t; + t = out[3]; out[3] = out[23]; out[23] = t; + } + + if (delay1) { + /* shift through delay_line */ + for (j = 0; j < LSUB_RAW+LSUB_Q+LSUB_P; j++) { + if ((j % MAX_SUB_DEL) != 0) { + t = sub_delay_line[(sub_del_index) % MAX_SUB_DEL][j]; + sub_delay_line[(sub_del_index + j) % MAX_SUB_DEL][j] = out[j]; + out[j] = t; + } + } + } + sub_del_index++; + out += LSUB_RAW+LSUB_Q+LSUB_P; + in += LSUB_RAW; + } + return 0; +} + +int +do_decode_sub( + unsigned char in[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME], + unsigned char out[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME], + int delay1, int permute) +{ + int i; + + if (in == out) return -1; + + for (i = 0; i < PACKETS_PER_SUBCHANNELFRAME; i++) { + int j; + unsigned char t; + + if (delay1) { + /* shift through delay_line */ + for (j = 0; j < LSUB_RAW+LSUB_Q+LSUB_P; j++) { + if ((j % MAX_SUB_DEL) != MAX_SUB_DEL-1) { + t = sub_delay_line[(sub_del_index) % MAX_SUB_DEL][j]; + sub_delay_line[(sub_del_index + (MAX_SUB_DEL - j)) % MAX_SUB_DEL][j] = in[j]; + in[j] = t; + } + } + } + + if (permute) { + /* permute */ + t = in[1]; in[1] = in[18]; in[18] = t; + t = in[2]; in[2] = in[ 5]; in[ 5] = t; + t = in[3]; in[3] = in[23]; in[23] = t; + } + + /* build P parity */ + decode_LSUB_P(in); + + /* build Q parity */ + decode_LSUB_Q(in); + + memcpy(out, in, LSUB_QRAW); + memcpy(out+LSUB_QRAW, in+LSUB_QRAW+LSUB_Q, LSUB_RAW-LSUB_QRAW); + + sub_del_index++; + in += LSUB_RAW+LSUB_Q+LSUB_P; + out += LSUB_RAW; + } + return 0; +} + +static int sectortype = MODE_0; +int get_sector_type(void) +{ + return sectortype; +} + +int set_sector_type(int st) +{ + switch(st) { + case MODE_0: + case MODE_1: + case MODE_2: + case MODE_2_FORM_1: + case MODE_2_FORM_2: + sectortype = st; + default: + return -1; + } + return 0; +} + +/* ------------- --------------*/ -- cgit v1.2.3