All pastes #448317 Raw Edit

Someone

public c v1 · immutable
·published 2007-04-19 16:02 UTC
rendered paste body
/******************************//* N64 Zelda ROM Decompressor *//******************************/#include <stdio.h>#include "yaz0.c"int main (){	// debug and shit	short debug = 0;		// Configuration	char command[2000];	char input_filename[]  = "your input file";	char output_filename[] = "your output file";	int output_size        = 64*1024*1024;	int input_size         = 32*1024*1024;	int file_table_start   = 0x0001A650;	int file_table_end     = 0x000208A0;	int a=0,b,c,d,e,f,g,h,i;	FILE *handle,*output;	unsigned int current_file1_start;	unsigned int current_file1_end;	unsigned int current_file2_start;	unsigned int current_file2_end;		// Contents	unsigned char *file_contents;	unsigned char *yaz0_decompress;	unsigned char *file_table;		// Begin	printf("Ready to decompress. Reading from \"%s\" and writing to \"%s\". Please ensure that this is ok, and press enter to continue.",input_filename,output_filename);	getchar();		// Open file for reading	handle = fopen(input_filename,"rb");	output = fopen(output_filename,"rb+");		// Check	if(!handle){		printf("Handle invalid"); exit(0);}else	if(!output){		printf("Output invalid"); exit(0);}		// Status update	printf("\nPadding output file... ");		// Make output filename 64MB	sprintf(command,"cp \"%s\" \"%s\" -f",input_filename,output_filename);	system(command);	fseek(output,32*1024*1024,SEEK_SET);	for(i=0;i<(32*1024*1024);i++)	{		fputc(0x00,output);	}		// Status	printf("Done.\nReading file offset table into RAM... ");		// Go	fseek(handle,file_table_start, SEEK_SET);	file_table = (unsigned char *)malloc(file_table_end-file_table_start);		// Read	for(i=0;i<(file_table_end-file_table_start);i++)	{		file_table[i]=fgetc(handle);	}		// Status	printf("Done. %i entries. \nProcessing.\n", ((file_table_end-file_table_start)/16));		// Loop through	for(i=0;i<(file_table_end-file_table_start);i+=16)	{		printf("NL");		// Read next offsets from table		current_file1_start = file_table[i];		current_file1_start = (current_file1_start<<8)|file_table[i+1];		current_file1_start = (current_file1_start<<8)|file_table[i+2];		current_file1_start = (current_file1_start<<8)|file_table[i+3];		current_file1_end = file_table[i+4];		current_file1_end = (current_file1_end<<8)|file_table[i+5];		current_file1_end = (current_file1_end<<8)|file_table[i+6];		current_file1_end = (current_file1_end<<8)|file_table[i+7];		current_file2_start = file_table[i+8];		current_file2_start = (current_file2_start<<8)|file_table[i+9];		current_file2_start = (current_file2_start<<8)|file_table[i+10];		current_file2_start = (current_file2_start<<8)|file_table[i+11];		current_file2_end = file_table[i+12];		current_file2_end = (current_file2_end<<8)|file_table[i+13];		current_file2_end = (current_file2_end<<8)|file_table[i+14];		current_file2_end = (current_file2_end<<8)|file_table[i+15];				// Modify offset table		file_table[i+8]=file_table[i];		file_table[i+9]=file_table[i+1];		file_table[i+10]=file_table[i+2];		file_table[i+11]=file_table[i+3];		file_table[i+12]=0x00;		file_table[i+13]=0x00;		file_table[i+14]=0x00;		file_table[i+15]=0x00;				// Debug		printf("%08X - %08X | %08X - %08X ",current_file1_start,current_file1_end,current_file2_start,current_file2_end,current_file2_start,current_file2_end);						// Do not compress		if(current_file2_end==0x00&&current_file2_start!=0x00&&current_file2_end!=0xFFFFFFFF)		{			printf("1 - ");			// Set aside RAM for next file			c=(current_file1_end-current_file1_start);			file_contents=(unsigned char*)malloc(c);			if(!file_contents){exit(0);}						// Read into RAM			if(fseek(handle,current_file2_start,SEEK_SET)){			printf("error code 1");getchar();exit(0);}			for(f=0;f<c;f++)			{				file_contents[f]=fgetc(handle);			}						// Write to other file			if(fseek(output,current_file1_start,SEEK_SET))			{printf("error code 3");getchar();exit(0);}			for(f=0;f<c;f++)			{				fputc(file_contents[f],output);			}						// Free RAM			free(file_contents);		}		else				// Decompress		if(current_file2_end!=0xFFFFFFFF&&current_file2_end!=0x00&&current_file1_end!=0x00)		{			printf("2 - ");			// Set aside RAM for yaz0 block			c=(current_file2_end-current_file2_start)-16/*we don't need the header*/;			b=(current_file1_end-current_file1_start);			file_contents  =(unsigned char*)malloc(c);			yaz0_decompress=(unsigned char*)malloc(b);			if(!file_contents||!yaz0_decompress){			printf("error code 2");getchar();exit(0);}						// Read data into RAM			if(fseek(handle,current_file2_start+16,SEEK_SET)){exit(0);}			for(f=0;f<c;f++)			{				file_contents[f]=fgetc(handle);			}						// Decompress			decode(file_contents,yaz0_decompress,b);						// Free RAM			free(file_contents);						// Write to output			if(fseek(output,current_file1_start,SEEK_SET)){exit(0);}			for(f=0;f<b;f++)			{				fputc(yaz0_decompress[f],output);			}						// Free			free(yaz0_decompress);		}		else		{			printf("No: - :");		}							// Print		printf("\n");		/*fflush(stdout);*/a++;	}		// write offset table back to ROM	fseek(output,file_table_start,SEEK_SET);	for(i=0;i<(file_table_end-file_table_start);i++)	{		fputc(file_table[i],output);	}		// Done	fclose(output);	fclose(handle);	printf("\nFinished [%i].",a);getchar();	// Return	return 1;}