Guest User

Aikku - DSFS Conversion Tool

a guest
Mar 8th, 2010
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. /******************************/
  2. #include "stdafx.h"
  3. /******************************/
  4.  
  5. u32 getChar(FILE *file) {
  6.     if(!feof(file)) return fgetc(file);
  7.    
  8.     return 0;
  9. }
  10.  
  11. /******************************/
  12.  
  13. int main(int argc, char *argv[]) {
  14.     if(!--argc) {
  15.         printf(
  16.             "dsfs - DS-Slot Based Filesystem for DS\n"
  17.             "Usage: dsfs file.nds [file1 [file2 [...]]]\n"
  18.             "Will modify DS FAT and append data to DS file.\n"
  19.         ); return 1;
  20.     } if(!--argc) {
  21.         printf("No files to add.\n");
  22.         return 1;
  23.     }
  24.    
  25.     //Load DS and temp file
  26.     FILE *ndsFile = fopen(argv[1], "r+b");
  27.     FILE *tmpFile = fopen("dsfstmp.$$$", "w+b");
  28.     int ndsEnd;
  29.    
  30.     /* Double check file */ {
  31.         if(!ndsFile) {
  32.             printf("Could not open DS file!\n");
  33.             return -1;
  34.         }
  35.        
  36.         if(!tmpFile) {
  37.             printf("Could not create temp. file!\n");
  38.             return -1;
  39.         }
  40.     }
  41.    
  42.     /* Find DSFAT */ {
  43.         int fpos = 0;
  44.         char magic[] = "dsfstab\0";
  45.        
  46.         fseek(ndsFile, 0, SEEK_END);
  47.         int flen = ftell(ndsFile); rewind(ndsFile);
  48.         ndsEnd = (flen + 0x1FF) &~ 0x1FF;
  49.        
  50.         char *ndsdata = (char*)malloc(flen);
  51.         if(!ndsdata) {
  52.             printf("Out of memory.\n");
  53.             return -1;
  54.         } fread(ndsdata, flen, 1, ndsFile);
  55.        
  56.         for(int i=0;i<flen;i++) {
  57.             for(int j=0;j<8;j++) {
  58.                 if( ndsdata[i+j] != magic[j]) break;
  59.                 if(!ndsdata[i+j]) {
  60.                     fpos = i;
  61.                     break;
  62.                 }
  63.             }
  64.         }
  65.        
  66.         if(!fpos) {
  67.             printf("DSFS not found!\n");
  68.             return -1;
  69.         }
  70.        
  71.         free(ndsdata);
  72.         fseek(ndsFile, fpos, SEEK_SET);
  73.     }
  74.    
  75.     for(int i=0;i<argc;i++) {
  76.         FILE *tFile = fopen(argv[2+i], "rb");
  77.        
  78.         fseek(tFile, 0, SEEK_END);
  79.         int fSize = ftell(tFile); rewind(tFile);
  80.        
  81.         /* Write to DS file */ {
  82.             char vname[256]; strcpy(vname, argv[2+i]);
  83.            
  84.             char *pos = strrchr(vname, '\\');
  85.             if(pos) strcpy(vname, pos+1);
  86.             pos = strrchr(vname, '/');
  87.             if(pos) strcpy(vname, pos+1);
  88.            
  89.             for(u32 j=0;j<32-8;j++) {
  90.                 if(j <= strlen(vname)) fputc(vname[j], ndsFile);
  91.                 else fputc(0, ndsFile);
  92.             }
  93.            
  94.             u32 src = ftell(tmpFile) + ndsEnd;
  95.            
  96.             u32 bsize = (fSize+0x1FF) &~ 0x1FF;
  97.            
  98.             fwrite(&src, 4, 1, ndsFile);
  99.             fwrite(&bsize, 4, 1, ndsFile);
  100.         }
  101.        
  102.         while(fSize--) {
  103.             fputc(getChar(tFile), tmpFile);
  104.         }
  105.        
  106.         while(ftell(tmpFile) & 0x1FF) fputc(0, tmpFile);
  107.     }
  108.    
  109.     /* Pad DS file */ {
  110.         fseek(ndsFile, 0, SEEK_END);
  111.         int pos = ftell(ndsFile) & 0x1FF;
  112.         int len = (0x200 - pos) & 0x1FF;
  113.        
  114.         while(len--) {
  115.             fputc(0, ndsFile);
  116.         }
  117.     }
  118.    
  119.     /* Concatenate */ {
  120.         rewind(tmpFile);
  121.        
  122.         while(!feof(tmpFile)) {
  123.             fputc(fgetc(tmpFile), ndsFile);
  124.         }
  125.     }
  126.    
  127.     fclose(tmpFile);
  128.     fclose(ndsFile);
  129.    
  130.     remove("dsfstmp.$$$");
  131. }
  132.  
  133. /******************************/
Advertisement
Add Comment
Please, Sign In to add comment