User:Remark/dec-flipbook.c

From DSiBrew
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
// dec_flipbook
// written by remark
// thanks to steven & ludo

#include <stdio.h>
#include <stdlib.h>

char key [0x40] = {
0xF7, 0x4C, 0x6A, 0x3A, 0xFB, 0x82, 0xA6, 0x37, 0x6E, 0x11, 0x38, 0xCF, 0xA0, 0xDD, 0x85, 0xC0,
0xC7, 0x9B, 0xC4, 0xD8, 0xDD, 0x28, 0x8A, 0x87, 0x53, 0x20, 0xEE, 0xE0, 0x0B, 0xEB, 0x43, 0xA0,
0xDB, 0x55, 0x0F, 0x75, 0x36, 0x37, 0xEB, 0x35, 0x6A, 0x34, 0x7F, 0xB5, 0x0F, 0x99, 0xF7, 0xEF,
0x43, 0x25, 0xCE, 0xA0, 0x29, 0x46, 0xD9, 0xD4, 0x4D, 0xBB, 0x04, 0x66, 0x68, 0x08, 0xF1, 0xF8
};

#define ERR(x ...) { printf(x); exit(1); }
#define PERR(x) { perror(x); exit(1); }

int fsize(FILE *f)
{
  int pos = ftell (f);
  fseek (f, 0, SEEK_END);
  int end = ftell (f);
  fseek (f, pos, SEEK_SET);
  return end;
}


int main(int argc, char* argv[])
{
  if(argc != 3)
  {
    printf("usage: %s <file_in> <file_out>\n", argv[0]);
    return 0;
  }
  
  FILE* f = fopen(argv[1], "rb");
  if(!f)
  	PERR("fopen");
  	
  int len = fsize(f);
  //char* buf = malloc(len);
  char* buf = (char*) malloc(len);
  if(!buf)
  {
  	fclose(f);
  	PERR("malloc");
  }
  
  if(fread(buf,len,1,f) != 1)
  {
  	fclose(f);
  	free(buf);
  	PERR("fread");
  }
  
  int i;
  for(i=0; i<len; i++)
  {
    buf[i] ^= key[i%0x40];
  }
  
  FILE* fout = fopen(argv[2], "wb+");
  if(!fout) 
  {
  	fclose(f);
  	free(buf);
  	PERR("fopen");
  }
  	
  if(fwrite(buf, len, 1, fout) != 1)
  {
  	fclose(f);
  	free(buf);
  	fclose(fout);
  	PERR("fwrite");
  }
  
  free(buf);
  fclose(f);
  fclose(fout);
  
  return 0;
}