stoneharry

Untitled

Apr 6th, 2013
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.93 KB | None | 0 0
  1. #include <fstream>
  2. #include <ostream>
  3. #include <Windows.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <vector>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. const unsigned char data[24] = {
  12.     0x18, 0x00, 0x04, 0x01,
  13.     0x00, 0x00, 0x00, 0x00,
  14.     0x5E, 0x00, 0x00, 0x00,
  15.     0x00, 0x00, 0x00, 0x00,
  16.     0x00, 0x00, 0x00, 0x00
  17. };
  18.  
  19. struct UpdateFileHeader
  20. {
  21.    UINT16 HdrSize; // always 0x0018
  22.    BYTE override; // 0x01 => override with new file; 0x04 => update file (bsdiff)
  23.    BYTE version; // always 0x04
  24.    UINT32 unknown3;
  25.    UINT32 OldFileSize; // file size before the update (uncompressed)
  26.    UINT32 NewFileSize; // size of the entire file after the update is applied (uncompressed)
  27.    UINT32 unknown6; // always 0
  28.    UINT32 unknown7; // always 0
  29. };
  30.  
  31. struct SearchFile
  32. {
  33.     typedef std::vector<std::string> FileNameArray;
  34.     FileNameArray files;
  35.  
  36.     FileNameArray::iterator begin()
  37.     {
  38.         return files.begin();
  39.     }
  40.  
  41.     FileNameArray::iterator end()
  42.     {
  43.         return files.end();
  44.     }
  45.  
  46.     int count() const
  47.     {
  48.         return (int)files.size();
  49.     }
  50.  
  51.     std::string operator[](int index)
  52.     {
  53.         return files[index];
  54.     }
  55.  
  56.     void operator()(const std::string &path, const std::string &pattern)
  57.     {
  58.         WIN32_FIND_DATA wfd;
  59.         HANDLE hf;
  60.         std::string findwhat;
  61.         std::vector<std::string> dir;
  62.  
  63.         findwhat = path + "\\*";  // directory
  64.  
  65.         hf = FindFirstFile(findwhat.c_str(), &wfd);
  66.         while (hf != INVALID_HANDLE_VALUE)
  67.         {
  68.             if (wfd.cFileName[0] != '.' && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  69.             {
  70.                 std::string found;
  71.  
  72.                 found = path + "\\" + wfd.cFileName;
  73.                 dir.push_back(found);
  74.             }
  75.  
  76.             if (!FindNextFile(hf, &wfd))
  77.             {
  78.                 FindClose(hf);
  79.                 hf = INVALID_HANDLE_VALUE;
  80.             }
  81.         }
  82.  
  83.         findwhat = path + "\\" + pattern;  // files
  84.  
  85.         hf = FindFirstFile(findwhat.c_str(), &wfd);
  86.         while (hf != INVALID_HANDLE_VALUE)
  87.         {
  88.             if (wfd.cFileName[0] != '.' && !(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  89.             {
  90.                 std::string found;
  91.  
  92.                 found = path + "\\" + wfd.cFileName;
  93.                 files.push_back(found);
  94.             }
  95.  
  96.             if (!FindNextFile(hf, &wfd))
  97.             {
  98.                 FindClose(hf);
  99.                 hf = INVALID_HANDLE_VALUE;
  100.             }
  101.         }
  102.  
  103.         // continue with directories
  104.         for (std::vector<std::string>::iterator it = dir.begin(); it != dir.end(); ++it)
  105.             this->operator()(*it, pattern);
  106.     }
  107. };
  108.  
  109. int main(void)
  110. {
  111.     SearchFile sf;
  112.  
  113.     sf("C:\\Users\\Harry\\Desktop\\test3\\", "*.*");
  114.  
  115.     for (int i = 0; i != sf.count(); ++i)
  116.     {
  117.         string path(sf[i].c_str());
  118.         string filename;
  119.  
  120.         size_t pos = path.find_last_of("\\");
  121.         if(pos != string::npos)
  122.             filename.assign(path.begin() + pos + 1, path.end());
  123.         else
  124.             filename = path;
  125.  
  126.         ofstream OutFile;
  127.         OutFile.open("C:\\Users\\Harry\\Desktop\\test5\\" + filename, ios::binary);
  128.  
  129.         UpdateFileHeader test;
  130.         test.HdrSize = 18;
  131.         test.NewFileSize = 0;
  132.         test.OldFileSize = 0;
  133.         test.override = 4;
  134.         test.unknown3 = 0;
  135.         test.unknown6 = 0;
  136.         test.unknown7 = 0;
  137.         test.version = 4;
  138.  
  139.         const char* output = reinterpret_cast<const char*>(data);
  140.  
  141.         OutFile.write(output, 24);
  142.  
  143.         ifstream file(sf[i].c_str(), ios::in | ios::binary | ios::ate);
  144.         ifstream::pos_type fileSize;
  145.         char* fileContents;
  146.         if(file.is_open())
  147.         {
  148.             fileSize = file.tellg();
  149.             test.NewFileSize = fileSize;
  150.             fileContents = new char[fileSize];
  151.             file.seekg(0, ios::beg);
  152.             if(!file.read(fileContents, fileSize))
  153.             {
  154.                 printf("Failed to read.\n");
  155.             }
  156.             file.close();
  157.            
  158.             OutFile.write(fileContents, fileSize);
  159.  
  160.             delete[] fileContents;
  161.         }
  162.  
  163.         OutFile.close();
  164.     }
  165.  
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment