Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NDS
- #define ENABLEWR
- #include "main.h"
- #include "memory.h"
- #include "2d.h"
- #include "math.h"
- #include "3dstypes.h"
- #include "constants.h"
- #include <stdio.h>
- #include <string.h>
- #include "crypto.h"
- #include "FS.h"
- /* edited by xerpi */
- void wcstrcpy(wchar_t *destination, const wchar_t *source)
- {
- while (*source != L'\0') {
- *destination++ = *source++;
- }
- *destination = L'\0';
- }
- wchar_t *wcstrcat(wchar_t *destination, const wchar_t *source)
- {
- wchar_t *dest = destination;
- while (*dest != L'\0') dest++;
- while (*source != L'\0') {
- *dest++ = *source++;
- }
- return destination;
- }
- int n_digits(int n)
- {
- int cnt = 1;
- while (n > 9) {
- n /= 10;
- cnt++;
- }
- return cnt;
- }
- void int_to_wstr(int n, wchar_t *dest)
- {
- #define UTF16_0 0x0030
- int n_dig = n_digits(n);
- int i;
- for (i = 0; i < n_dig; i++) {
- int cur_digit = n%10;
- n /= 10;
- *dest++ = UTF16_0 + cur_digit;
- }
- *dest = L'\0';
- }
- void createpad(void *counter, void *keyY, void *filename, u32 megabytes, u8 padnum)
- {
- #define MAX_MB 256
- #define BLOCK_SIZE 0x1000 //only 4KB, unless we can find a larger open spot in ARM9 mem.
- #define AES_BIG_INPUT 1
- #define AES_NORMAL_INPUT 4
- u8 ctr[16];
- u8 buffer[BLOCK_SIZE];
- u8 zeroed_inp[16];
- memcpy(ctr, counter, 16);
- memset(zeroed_inp, 0, 16);
- int steps = megabytes/MAX_MB;
- if (megabytes%MAX_MB != 0) steps++;
- print("Creating pad ", 190, (padnum)*10, 0, 0, 255);
- print_u8(padnum, 300, (padnum)*10, 0, 0, 255);
- print("Completed", 00, 210, 255, 0, 0);
- print("Out of", 00, 220, 255, 0, 0);
- print_u32(megabytes*1024*1024, 100, 220, 255, 0, 0);
- u32 mb_cnt = 0;
- int steps_i;
- for (steps_i = 0; steps_i < steps; steps_i++) {
- wchar_t filename2[32];
- wcstrcpy(filename2, filename);
- wchar_t steps_wstr[4] = {0x002E, 0x0};
- int_to_wstr(steps_i, &steps_wstr[1]);
- wcstrcat(filename2, steps_wstr);
- u32 handle = FileOpen(filename2);
- setup_aeskey(0x2C, AES_BIG_INPUT|AES_NORMAL_INPUT, keyY);
- use_aeskey(0x2C);
- u32 n_mb = (steps_i < (steps-1)) ? MAX_MB :
- megabytes - steps*MAX_MB;
- u32 n_bytes = n_mb*1024*1024;
- u32 i, j;
- for (i = 0; i < n_bytes; i += BLOCK_SIZE) {
- u32 n_block_size = (i+BLOCK_SIZE > n_bytes) ?
- n_bytes - i:
- BLOCK_SIZE;
- for (j = 0; j < n_block_size; j += 16) {
- set_ctr(AES_BIG_INPUT|AES_NORMAL_INPUT, ctr);
- aes_decrypt(zeroed_inp, (void *)(buffer+j), ctr, 1);
- add_ctr(ctr, 1);
- }
- FileWriteOffset(handle, buffer, n_block_size, steps_i*MAX_MB + i);
- if (!(i&0xFFFFF)) {
- print_u32(i, 100, 210, 255, 0, 0);
- }
- }
- }
- blank(00,210,200,20);
- print( "Done!", 340,(padnum)*10,0,0,255);
- }
- int main()
- {
- blank(0,0,400,240);
- u8 ncchinfo[2004]; //enough room for 20 file infos
- u32 entries[4];
- u32 handle = FileOpen( L"/ncchinfo.bin");
- FileRead( handle, entries, 16);
- if(!entries[0] || entries[0]>20)
- {
- print( "Nothing to do. :/",80,10,0,0,255);
- return 0;
- }
- FileRead( handle, ncchinfo, entries[0]*100+4);
- /*
- 4 bytes number of entries
- entry:
- 16 bytes CTR
- 16 bytes keyY
- 4 bytes length in megabytes(round up)
- 64 bytes output filename in utf16(could use something like "/ncchcode1.romfs.xorpad")
- */
- #define entry_ctr 0+4 //+4 because first int in the array is not an entry
- #define entry_keyY 16+4
- #define entry_megabytes 32+4
- #define entry_filename 36+4
- u8 i = 0;
- u32 e = 0;
- for(i = 0; i< entries[0]; i++)
- {
- e = i*100;
- createpad((void *)(ncchinfo+e+entry_ctr), (void *)(ncchinfo+e+entry_keyY), (void *)(ncchinfo+e+entry_filename), *(u32 *)(ncchinfo+e+entry_megabytes), i+1);
- }
- print( "Finished!", 10,10,0,0,255);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement