Advertisement
xerpi

padgen main.c

Aug 30th, 2014
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.10 KB | None | 0 0
  1. #define NDS
  2. #define ENABLEWR
  3. #include "main.h"
  4. #include "memory.h"
  5. #include "2d.h"
  6. #include "math.h"
  7. #include "3dstypes.h"
  8. #include "constants.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "crypto.h"
  12. #include "FS.h"
  13.  
  14. /* edited by xerpi */
  15.  
  16. void wcstrcpy(wchar_t *destination, const wchar_t *source)
  17. {
  18.     while (*source != L'\0') {
  19.         *destination++ = *source++;
  20.     }
  21.     *destination = L'\0';
  22. }
  23.  
  24. wchar_t *wcstrcat(wchar_t *destination, const wchar_t *source)
  25. {
  26.     wchar_t *dest = destination;
  27.     while (*dest != L'\0') dest++;
  28.     while (*source != L'\0') {
  29.         *dest++ = *source++;
  30.     }
  31.     return destination;
  32. }
  33.  
  34. int n_digits(int n)
  35. {
  36.     int cnt = 1;
  37.     while (n > 9) {
  38.         n /= 10;
  39.         cnt++;
  40.     }
  41.     return cnt;
  42. }
  43.  
  44. void int_to_wstr(int n, wchar_t *dest)
  45. {
  46.     #define UTF16_0 0x0030
  47.     int n_dig = n_digits(n);
  48.     int i;
  49.     for (i = 0; i < n_dig; i++) {
  50.         int cur_digit = n%10;
  51.         n /= 10;
  52.         *dest++ = UTF16_0 + cur_digit;
  53.     }
  54.     *dest = L'\0';
  55. }
  56.  
  57. void createpad(void *counter, void *keyY, void *filename, u32 megabytes, u8 padnum)
  58. {
  59.     #define MAX_MB 256
  60.     #define BLOCK_SIZE 0x1000 //only 4KB, unless we can find a larger open spot in ARM9 mem.
  61.     #define AES_BIG_INPUT      1
  62.     #define AES_NORMAL_INPUT   4
  63.        
  64.     u8 ctr[16];
  65.     u8 buffer[BLOCK_SIZE];
  66.     u8 zeroed_inp[16];
  67.     memcpy(ctr, counter, 16);
  68.     memset(zeroed_inp, 0, 16);
  69.    
  70.     int steps = megabytes/MAX_MB;
  71.     if (megabytes%MAX_MB != 0) steps++;
  72.    
  73.     print("Creating pad ", 190, (padnum)*10, 0, 0, 255);
  74.     print_u8(padnum, 300, (padnum)*10, 0, 0, 255);
  75.    
  76.     print("Completed", 00, 210, 255, 0, 0);
  77.     print("Out of", 00, 220, 255, 0, 0);
  78.     print_u32(megabytes*1024*1024,  100, 220, 255, 0, 0);
  79.    
  80.     u32 mb_cnt = 0;
  81.     int steps_i;
  82.     for (steps_i = 0; steps_i < steps; steps_i++) {
  83.         wchar_t filename2[32];
  84.         wcstrcpy(filename2, filename);
  85.         wchar_t steps_wstr[4] = {0x002E, 0x0};
  86.         int_to_wstr(steps_i, &steps_wstr[1]);
  87.         wcstrcat(filename2, steps_wstr);
  88.        
  89.         u32 handle = FileOpen(filename2);
  90.         setup_aeskey(0x2C, AES_BIG_INPUT|AES_NORMAL_INPUT, keyY);
  91.         use_aeskey(0x2C);
  92.  
  93.         u32 n_mb = (steps_i < (steps-1)) ? MAX_MB :
  94.             megabytes - steps*MAX_MB;
  95.         u32 n_bytes = n_mb*1024*1024;
  96.        
  97.         u32 i, j;
  98.         for (i = 0; i < n_bytes; i += BLOCK_SIZE) {
  99.            
  100.             u32 n_block_size = (i+BLOCK_SIZE > n_bytes) ?
  101.                 n_bytes - i:
  102.                 BLOCK_SIZE;
  103.            
  104.             for (j = 0; j < n_block_size; j += 16) {
  105.                 set_ctr(AES_BIG_INPUT|AES_NORMAL_INPUT, ctr);
  106.                 aes_decrypt(zeroed_inp, (void *)(buffer+j), ctr, 1);
  107.                 add_ctr(ctr, 1);
  108.             }
  109.             FileWriteOffset(handle, buffer, n_block_size, steps_i*MAX_MB + i);
  110.             if (!(i&0xFFFFF)) {
  111.                 print_u32(i, 100, 210, 255, 0, 0);
  112.             }
  113.         }
  114.     }
  115.  
  116.     blank(00,210,200,20);
  117.     print( "Done!", 340,(padnum)*10,0,0,255);
  118. }
  119.  
  120. int main()
  121. {
  122.  
  123.     blank(0,0,400,240);
  124.  
  125.     u8 ncchinfo[2004]; //enough room for 20 file infos
  126.  
  127.     u32 entries[4];
  128.     u32 handle = FileOpen( L"/ncchinfo.bin");
  129.     FileRead( handle, entries, 16);
  130.    
  131.     if(!entries[0] || entries[0]>20)
  132.     {
  133.         print( "Nothing to do. :/",80,10,0,0,255);
  134.         return 0;
  135.     }
  136.     FileRead( handle, ncchinfo, entries[0]*100+4);
  137. /*
  138. 4 bytes number of entries
  139.  
  140. entry:
  141.         16 bytes CTR
  142.         16 bytes keyY
  143.         4 bytes length in megabytes(round up)
  144.         64 bytes output filename in utf16(could use something like "/ncchcode1.romfs.xorpad")
  145. */
  146.  
  147.     #define entry_ctr 0+4 //+4 because first int in the array is not an entry
  148.     #define entry_keyY 16+4
  149.     #define entry_megabytes 32+4
  150.     #define entry_filename 36+4
  151.     u8 i = 0;
  152.     u32 e = 0;
  153.     for(i = 0; i< entries[0]; i++)
  154.     {
  155.         e = i*100;
  156.         createpad((void *)(ncchinfo+e+entry_ctr), (void *)(ncchinfo+e+entry_keyY), (void *)(ncchinfo+e+entry_filename), *(u32 *)(ncchinfo+e+entry_megabytes), i+1);
  157.     }
  158.  
  159.     print( "Finished!", 10,10,0,0,255);
  160.  
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement