Guest User

Untitled

a guest
Aug 30th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <cstdint>
  5. #include <sstream>
  6. using namespace std;
  7.  
  8. typedef uint32_t u32;
  9. typedef uint8_t u8;
  10.  
  11. #define BLOCKS_PER_MEGABYTE 1024*1024/0x10
  12.  
  13. struct ctrCommand {
  14.     u32 ctr[4];
  15.     u32 keyY[4];
  16.     u32 megabytes;
  17.     wchar_t filename[32];
  18. };
  19. struct ctrCommandsList {
  20.     int count;
  21.     ctrCommand* items;
  22. };
  23.  
  24. string padHex(u32 n) {
  25.     stringstream s;
  26.     s << setfill('0') << setw(8) << hex << n;
  27.     return s.str();
  28. }
  29. string itos(int n) {
  30.     stringstream s;
  31.     s << n;
  32.     return s.str();
  33. }
  34.  
  35.  
  36. void add_ctr(void* ctr, u32 carry)
  37. {
  38.     u32 counter[4];
  39.     u8 *outctr = (u8 *) ctr;
  40.     u32 sum;
  41.     int i;
  42.  
  43.     for(i=0; i<4; i++){
  44.         counter[i] = (outctr[i*4+0]<<24) | (outctr[i*4+1]<<16) | (outctr[i*4+2]<<8) | (outctr[i*4+3]<<0);}
  45.  
  46.     for(i=3; i>=0; i--)
  47.     {
  48.         sum = counter[i] + carry;
  49.         if (sum < counter[i]){
  50.             carry = 1;}
  51.         else{
  52.             carry = 0;}
  53.         counter[i] = sum;
  54.     }
  55.  
  56.     for(i=0; i<4; i++)
  57.     {
  58.         outctr[i*4+0] = counter[i]>>24;
  59.         outctr[i*4+1] = counter[i]>>16;
  60.         outctr[i*4+2] = counter[i]>>8;
  61.         outctr[i*4+3] = counter[i]>>0;
  62.     }
  63. }
  64.  
  65.  
  66. void showCommandInfo(ctrCommand* command) {
  67.     cout << "counter:"
  68.             << " " << padHex(command->ctr[3])
  69.             << " " << padHex(command->ctr[2])
  70.             << " " << padHex(command->ctr[1])
  71.             << " " << padHex(command->ctr[0])
  72.             << endl;
  73.     cout << "keyY:   "
  74.             << " " << padHex(command->keyY[3])
  75.             << " " << padHex(command->keyY[2])
  76.             << " " << padHex(command->keyY[1])
  77.             << " " << padHex(command->keyY[0])
  78.             << endl;
  79.     cout << "length: " << command->megabytes << " MiB" << endl;
  80.     wcout << "filename: " << command->filename << endl;
  81. }
  82.  
  83. ctrCommandsList splitCommand(ctrCommand* command, int chunkSize) {
  84.    
  85.     ctrCommandsList list;
  86.  
  87.     list.count = (command->megabytes+chunkSize-1)/chunkSize;
  88.     list.items = new ctrCommand[list.count];
  89.  
  90.     for (int i=0; i<list.count; i++) {
  91.         list.items[i] = *command;
  92.         list.items[i].megabytes = chunkSize;
  93.        
  94.         char c1 = itos(i/10).c_str()[0];
  95.         char c2 = itos(i%10).c_str()[0];
  96.  
  97.         int length = wcslen(list.items[i].filename);
  98.  
  99.         list.items[i].filename[length] = '.';
  100.         list.items[i].filename[length+1] = (wchar_t)c1;
  101.         list.items[i].filename[length+2] = (wchar_t)c2;
  102.  
  103.         add_ctr(command->ctr, chunkSize*BLOCKS_PER_MEGABYTE);
  104.     }
  105.  
  106.     if (command->megabytes % chunkSize != 0) {
  107.         list.items[list.count-1].megabytes = command->megabytes % chunkSize;
  108.     }
  109.  
  110.     return list;
  111. }
  112.  
  113. int main() {
  114.    
  115.     u32 entries;
  116.     string filename = "D:\\3ds\\ncchinfo.bin";
  117.     ifstream input;
  118.     ofstream output;
  119.     ctrCommand* command = new ctrCommand;
  120.  
  121.     input.open(filename, ifstream::in | ifstream::binary);
  122.  
  123.     input.read((char*)&entries, 4);
  124.  
  125.     cout << "This file has " << entries << " entries." << endl << endl;
  126.  
  127.     for (int i=0; i<entries; i++) {
  128.         input.read((char*)command, sizeof(ctrCommand));
  129.  
  130.         cout << "Entry #" << i << ":" << endl;
  131.         showCommandInfo(command);
  132.         ctrCommandsList list = splitCommand(command, 256);
  133.  
  134.         for (int j=0; j<list.count; j++) {
  135.             string newFilename = filename+"."+itos(i)+"."+itos(j);
  136.             cout << "Writing entry " << i << ", chunk " << j << " to " << newFilename << endl;
  137.             output.open(newFilename, ios::binary | ios::out);
  138.  
  139.             u32 one = 1;
  140.             output.write((char*)&one, 4);
  141.             output.write((char*)&(list.items[j]), sizeof(ctrCommand));
  142.  
  143.             output.close();
  144.         }
  145.  
  146.         cout << endl;
  147.  
  148.     }
  149.  
  150.     input.close();
  151.     cin.sync();
  152.     cin.get();
  153. }
Advertisement
Add Comment
Please, Sign In to add comment