Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************/
- #include "stdafx.h"
- /******************************/
- u32 getChar(FILE *file) {
- if(!feof(file)) return fgetc(file);
- return 0;
- }
- /******************************/
- int main(int argc, char *argv[]) {
- if(!--argc) {
- printf(
- "dsfs - DS-Slot Based Filesystem for DS\n"
- "Usage: dsfs file.nds [file1 [file2 [...]]]\n"
- "Will modify DS FAT and append data to DS file.\n"
- ); return 1;
- } if(!--argc) {
- printf("No files to add.\n");
- return 1;
- }
- //Load DS and temp file
- FILE *ndsFile = fopen(argv[1], "r+b");
- FILE *tmpFile = fopen("dsfstmp.$$$", "w+b");
- int ndsEnd;
- /* Double check file */ {
- if(!ndsFile) {
- printf("Could not open DS file!\n");
- return -1;
- }
- if(!tmpFile) {
- printf("Could not create temp. file!\n");
- return -1;
- }
- }
- /* Find DSFAT */ {
- int fpos = 0;
- char magic[] = "dsfstab\0";
- fseek(ndsFile, 0, SEEK_END);
- int flen = ftell(ndsFile); rewind(ndsFile);
- ndsEnd = (flen + 0x1FF) &~ 0x1FF;
- char *ndsdata = (char*)malloc(flen);
- if(!ndsdata) {
- printf("Out of memory.\n");
- return -1;
- } fread(ndsdata, flen, 1, ndsFile);
- for(int i=0;i<flen;i++) {
- for(int j=0;j<8;j++) {
- if( ndsdata[i+j] != magic[j]) break;
- if(!ndsdata[i+j]) {
- fpos = i;
- break;
- }
- }
- }
- if(!fpos) {
- printf("DSFS not found!\n");
- return -1;
- }
- free(ndsdata);
- fseek(ndsFile, fpos, SEEK_SET);
- }
- for(int i=0;i<argc;i++) {
- FILE *tFile = fopen(argv[2+i], "rb");
- fseek(tFile, 0, SEEK_END);
- int fSize = ftell(tFile); rewind(tFile);
- /* Write to DS file */ {
- char vname[256]; strcpy(vname, argv[2+i]);
- char *pos = strrchr(vname, '\\');
- if(pos) strcpy(vname, pos+1);
- pos = strrchr(vname, '/');
- if(pos) strcpy(vname, pos+1);
- for(u32 j=0;j<32-8;j++) {
- if(j <= strlen(vname)) fputc(vname[j], ndsFile);
- else fputc(0, ndsFile);
- }
- u32 src = ftell(tmpFile) + ndsEnd;
- u32 bsize = (fSize+0x1FF) &~ 0x1FF;
- fwrite(&src, 4, 1, ndsFile);
- fwrite(&bsize, 4, 1, ndsFile);
- }
- while(fSize--) {
- fputc(getChar(tFile), tmpFile);
- }
- while(ftell(tmpFile) & 0x1FF) fputc(0, tmpFile);
- }
- /* Pad DS file */ {
- fseek(ndsFile, 0, SEEK_END);
- int pos = ftell(ndsFile) & 0x1FF;
- int len = (0x200 - pos) & 0x1FF;
- while(len--) {
- fputc(0, ndsFile);
- }
- }
- /* Concatenate */ {
- rewind(tmpFile);
- while(!feof(tmpFile)) {
- fputc(fgetc(tmpFile), ndsFile);
- }
- }
- fclose(tmpFile);
- fclose(ndsFile);
- remove("dsfstmp.$$$");
- }
- /******************************/
Advertisement
Add Comment
Please, Sign In to add comment