Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <cstdint>
- #include <sstream>
- using namespace std;
- typedef uint32_t u32;
- typedef uint8_t u8;
- #define BLOCKS_PER_MEGABYTE 1024*1024/0x10
- struct ctrCommand {
- u32 ctr[4];
- u32 keyY[4];
- u32 megabytes;
- wchar_t filename[32];
- };
- struct ctrCommandsList {
- int count;
- ctrCommand* items;
- };
- string padHex(u32 n) {
- stringstream s;
- s << setfill('0') << setw(8) << hex << n;
- return s.str();
- }
- string itos(int n) {
- stringstream s;
- s << n;
- return s.str();
- }
- void add_ctr(void* ctr, u32 carry)
- {
- u32 counter[4];
- u8 *outctr = (u8 *) ctr;
- u32 sum;
- int i;
- for(i=0; i<4; i++){
- counter[i] = (outctr[i*4+0]<<24) | (outctr[i*4+1]<<16) | (outctr[i*4+2]<<8) | (outctr[i*4+3]<<0);}
- for(i=3; i>=0; i--)
- {
- sum = counter[i] + carry;
- if (sum < counter[i]){
- carry = 1;}
- else{
- carry = 0;}
- counter[i] = sum;
- }
- for(i=0; i<4; i++)
- {
- outctr[i*4+0] = counter[i]>>24;
- outctr[i*4+1] = counter[i]>>16;
- outctr[i*4+2] = counter[i]>>8;
- outctr[i*4+3] = counter[i]>>0;
- }
- }
- void showCommandInfo(ctrCommand* command) {
- cout << "counter:"
- << " " << padHex(command->ctr[3])
- << " " << padHex(command->ctr[2])
- << " " << padHex(command->ctr[1])
- << " " << padHex(command->ctr[0])
- << endl;
- cout << "keyY: "
- << " " << padHex(command->keyY[3])
- << " " << padHex(command->keyY[2])
- << " " << padHex(command->keyY[1])
- << " " << padHex(command->keyY[0])
- << endl;
- cout << "length: " << command->megabytes << " MiB" << endl;
- wcout << "filename: " << command->filename << endl;
- }
- ctrCommandsList splitCommand(ctrCommand* command, int chunkSize) {
- ctrCommandsList list;
- list.count = (command->megabytes+chunkSize-1)/chunkSize;
- list.items = new ctrCommand[list.count];
- for (int i=0; i<list.count; i++) {
- list.items[i] = *command;
- list.items[i].megabytes = chunkSize;
- char c1 = itos(i/10).c_str()[0];
- char c2 = itos(i%10).c_str()[0];
- int length = wcslen(list.items[i].filename);
- list.items[i].filename[length] = '.';
- list.items[i].filename[length+1] = (wchar_t)c1;
- list.items[i].filename[length+2] = (wchar_t)c2;
- add_ctr(command->ctr, chunkSize*BLOCKS_PER_MEGABYTE);
- }
- if (command->megabytes % chunkSize != 0) {
- list.items[list.count-1].megabytes = command->megabytes % chunkSize;
- }
- return list;
- }
- int main() {
- u32 entries;
- string filename = "D:\\3ds\\ncchinfo.bin";
- ifstream input;
- ofstream output;
- ctrCommand* command = new ctrCommand;
- input.open(filename, ifstream::in | ifstream::binary);
- input.read((char*)&entries, 4);
- cout << "This file has " << entries << " entries." << endl << endl;
- for (int i=0; i<entries; i++) {
- input.read((char*)command, sizeof(ctrCommand));
- cout << "Entry #" << i << ":" << endl;
- showCommandInfo(command);
- ctrCommandsList list = splitCommand(command, 256);
- for (int j=0; j<list.count; j++) {
- string newFilename = filename+"."+itos(i)+"."+itos(j);
- cout << "Writing entry " << i << ", chunk " << j << " to " << newFilename << endl;
- output.open(newFilename, ios::binary | ios::out);
- u32 one = 1;
- output.write((char*)&one, 4);
- output.write((char*)&(list.items[j]), sizeof(ctrCommand));
- output.close();
- }
- cout << endl;
- }
- input.close();
- cin.sync();
- cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment