/* SMCRen v1.5 (c) 1999 Gridle - renames all your SNES roms to their "real" names. */ /* This define must be used if compiling on lcc (use findfirst) */ //#define USE_FINDFIRST #include #include #include #include #include /* History: 0.x - not really SMCRen, but a proggy which dug up the rom name and wrote a 4Dos descript.ion about it (this option removed later) 1.0 - initial beta-test release, only renaming possible 1.1 - auto-converting from no-header to smc 1.2 - multiple images support 1.3 - relative paths understood (smcren blah\blah\blah.rom) - ported to win32 console also, needed using findfirst on every argument - the first official release 1.4 - lynx roms support - gameboy roms support (list only contains one rom) 1.5 - gameboy list */ /* Todo: - output dir - logging - "-verifyroms" - proper support for multiple rom types (this is a damn kludge right now) */ #ifndef TRUE #define TRUE -1 #define FALSE 0 #endif /* This define is wrapped to a lfn check function on DJGPP (dos), should be true for anything else */ #ifndef _USE_LFN #define _USE_LFN TRUE #endif #ifdef USE_FINDFIRST #include // lcc #endif #define VERSION "1.5" char realtitle[1000]; char crcfilename[1000]; char filetocheck[1000]; int multiformat; unsigned char *romdata; int output; // see I suck at coding char outputdir[1000]; int systemmode; #define SYSTEM_NONE 0 #define SYSTEM_SNES 1 #define SYSTEM_LYNX 2 #define SYSTEM_GAMEBOY 3 int filenamecopypath(char *withoutpath,char *withpath); void fetch_crc(unsigned int crc); int readfiles(const char* file, int *filesize); unsigned int calc_crc32 (unsigned int crc, const unsigned char *buf, unsigned int len); unsigned char smcheader[16]={ 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xBB, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 }; /* Add 0x00 until the header is 512 bytes large */ struct lynxhead { unsigned char magic[4]; unsigned short page_size_bank0; unsigned short page_size_bank1; unsigned short version; char cartname[32]; char manufname[16]; unsigned char rotation; unsigned char spare[5]; } lynxheader; int page_size0,page_size1,rotation; int main(int argc, char **argv) { unsigned char *data; FILE *fp; int size,totaltotalsize,anothersize; int current; int smc; unsigned int crc; int nf; int rena; int autoconvert; char filename[200]; char tempstring[200]; int i; int multiimage; #ifdef USE_FINDFIRST struct _finddata_t ffb; int done; long help; // <-- for expanding *.* etc. ourselves #endif //USE_FINDFIRST smc=FALSE; printf("SMCRen v%s (c) 1999 Gridle - renames all your SNES/LYNX/GB roms to their \"real\" names.\n\n",VERSION); if(argc<2) { printf( "Usage: SMCRen [-r] [-c]"/* [-o ]*/" \n" "(example: smcren -r *.smc *.fig, smcren -r -c sf*, smcren -c *.* -o crcfound\\)\n" "\n" "If -r is not specified, files will not be renamed. You will only see the\n" "suggestions of their real names. If -c is specified (will force -r), all files\n" "will be auto-converted to SMC format.\n" /* The roms which get be successfully\n" "converted/renamed can be moved to their own directory with -o.\n" "It is even possible to process several roms and output them to different\n" "directories: smcren -c -o snesready\\ snes\\* -o lynxready\\ lynx\\*\n"*/ "\n" /*"If you get a \"NOT FOUND FROM DATABASE\" message, please take up the CRC\n" "(redirect the output to a TXT file like SMCRen *.smc >blah.txt) and send it to\n" "gridle@mbnet.fi, preferably with the REAL name of the game. Do NOT send ROM\n" "images. The database file is crclist.txt, and it should be in the same\n" "directory as SMCRen.\n"*/ "The database file is crclist.txt, and it should be in the same directory as\n" "SMCRen.\n" ); exit(0); } /* Settings from the user */ rena=FALSE; autoconvert=FALSE; output=FALSE; current=1; while(current0) { systemmode=SYSTEM_SNES; readfiles(filename,&anothersize); crc = calc_crc32 (0, romdata, anothersize); free(romdata); strcpy(realtitle,"Not found!"); nf=TRUE; fetch_crc(crc); if (strcmp(realtitle,"Not found!")!=0) nf=FALSE; filenamecopypath(realtitle,filetocheck); if(nf==FALSE) { printf("%08x %s (multi) %s <- ",crc,filetocheck,realtitle); // Convert a multi-image to a single file! Gakk.. if(autoconvert) { printf("Auto-converting to SMC!\n"); readfiles(filename,&totaltotalsize); fp=fopen(realtitle,"wb"); fwrite(smcheader, sizeof(unsigned char), 16, fp); for(i=0;i<496;i++) fputc(0x00, fp); if (fwrite(romdata, sizeof (unsigned char), totaltotalsize, fp) == totaltotalsize) { fclose(fp); free(romdata); i=0; while(filename[i]!='.' && filename[i]!=0) i++; multiimage++; while(multiimage>0) { remove(filename); if(multiformat==0) filename[i-1]++; else filename[i+1]++; multiimage--; } } else { fclose(fp); free(romdata); printf("Error while writing to the new ROM.\n"); exit(0); } } else { if(rena) printf("Renaming!\nCan't rename. Please join your multiple images.\n"); else printf("Rename!\n"); } } else printf("%08x %s (multi) <- NOT FOUND FROM DATABASE!\n",crc,filetocheck); } else // printf("%08x %s\n",crc,filetocheck); printf("%08x %s <- NOT FOUND FROM DATABASE!\n",crc,filetocheck); } // enough about doing everything getnext: #ifdef USE_FINDFIRST done=_findnext(help,&ffb); } #endif // USE_FINDFIRST getout: current++; } return 0; } int readfiles(const char* file, int *filesize) { FILE *fp; int size,totalsize,totaltotalsize; int smc,header; int i; int multiimage; char filename[200]; if(systemmode==SYSTEM_SNES) { totalsize=size=0; i=0; multiimage=0; strcpy(filename,file); while(filename[i]!='.' && filename[i]!=0) i++; while(1==1) { fp=fopen(filename,"rb"); if(fp!=NULL) { fseek(fp,0,SEEK_END); size=ftell(fp); if((size % 1024) == 0) smc=FALSE; else if((size % 1024) == 512) smc=TRUE; else { printf("Problem!\n"); exit(0); /* !!!!!!!! */ } if(smc) totalsize+=(size-512); else totalsize+=(size); multiimage++; fclose(fp); } else { break; } if(multiformat==0) filename[i-1]++; else if(multiformat==1) filename[i+1]++; else break; } romdata = (unsigned char*)malloc(totalsize); if (!romdata) { fclose(fp); return -1; } totaltotalsize=totalsize; totalsize=0; i=0; strcpy(filename,file); while(filename[i]!='.' && filename[i]!=0) i++; while(1==1) { fp=fopen(filename,"rb"); if(fp!=NULL) { fseek(fp,0,SEEK_END); size=ftell(fp); if((size % 1024) == 0) smc=FALSE; else if((size % 1024) == 512) smc=TRUE; else { printf("Problem!\n"); exit(0); /* !!!!!!!! */ } if(smc) fseek(fp, 512, SEEK_SET); else fseek(fp, 0, SEEK_SET); if(smc) { if (fread(romdata+totalsize, sizeof (unsigned char), size-512, fp) != (size-512)) { free(romdata); fclose(fp); return -1; } } else { if (fread(romdata+totalsize, sizeof (unsigned char), size, fp) != size) { free(romdata); fclose(fp); return -1; } } if(smc) totalsize+=(size-512); else totalsize+=(size); fclose(fp); } else { break; } if(multiformat==0) filename[i-1]++; else if(multiformat==1) filename[i+1]++; else break; } *filesize = totaltotalsize; } if(systemmode==SYSTEM_LYNX || systemmode==SYSTEM_GAMEBOY) { totalsize=size=0; i=0; multiimage=0; strcpy(filename,file); fp=fopen(filename,"rb"); if(fp!=NULL) { fseek(fp,0,SEEK_END); size=ftell(fp); if((size % 128) == 0) header=FALSE; else if((size % 128) == 64) header=TRUE; else { printf("Problem!\n"); exit(0); /* !!!!!!!! */ } if(header) totalsize+=(size-64); else totalsize+=(size); fclose(fp); } romdata = (unsigned char*)malloc(totalsize); if (!romdata) { fclose(fp); return -1; } totaltotalsize=totalsize; totalsize=0; i=0; strcpy(filename,file); fp=fopen(filename,"rb"); if(fp!=NULL) { fseek(fp,0,SEEK_END); size=ftell(fp); if((size % 128) == 0) header=FALSE; else if((size % 128) == 64) header=TRUE; else { printf("Problem!\n"); exit(0); /* !!!!!!!! */ } if(header) fseek(fp, 64, SEEK_SET); else fseek(fp, 0, SEEK_SET); if(header) { if (fread(romdata+totalsize, sizeof (unsigned char), size-64, fp) != (size-64)) { free(romdata); fclose(fp); return -1; } } else { if (fread(romdata+totalsize, sizeof (unsigned char), size, fp) != size) { free(romdata); fclose(fp); return -1; } } if(header) totalsize+=(size-64); else totalsize+=(size); fclose(fp); } *filesize = totaltotalsize; } return 0; } void fetch_crc(unsigned int crc) { FILE *crclist; char buff[10]; int i,j; char paska[1000]; char tmp; sprintf(buff,"%08x",crc); crclist=fopen(crcfilename,"rt"); alkuf: if(feof(crclist)) goto outtahere; tmp=fgetc(crclist); if(feof(crclist)) goto outtahere; if(tmp=='#') { fgets(paska,900,crclist); j=strlen(paska); for(i=0;i> 8); } while (--len); return crc ^ 0xffffffffL; } int filenamecopypath(char *withoutpath,char *withpath) { char tempo[500]; int i; strcpy(tempo,withpath); i=strlen(tempo); while(tempo[i]!='/' && tempo[i]!='\\' && i>=0) i--; i++; tempo[i]=0; strcat(tempo,withoutpath); strcpy(withoutpath,tempo); return 0; }