stoneharry

Untitled

Feb 17th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.83 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. typedef unsigned long ULONG;
  10.  
  11. ULONG crc32_table[256];
  12. ULONG ulPolynomial = 0x04c11db7;
  13.  
  14. using namespace std;
  15.  
  16. struct UpdateFileHeader
  17. {
  18.    UINT16 HdrSize; // always 0x0018
  19.    UINT8 version; // always 0x04
  20.    UINT8 override; // 0x01 => override with new file; 0x04 => update file (bsdiff)
  21.    UINT32 crc32; // of the file before update
  22.    UINT32 OldFileSize; // file size before the update (uncompressed)
  23.    UINT32 NewFileSize; // size of the entire file after the update is applied (uncompressed)
  24.    UINT32 unknown6; // always 0
  25.    UINT32 unknown7; // always 0
  26. };
  27.  
  28. // Below begins copy + pasted code slightly modified
  29.  
  30. struct SearchFilea
  31. {
  32.     typedef std::vector<std::string> FileNameArray;
  33.     FileNameArray files;
  34.  
  35.     FileNameArray::iterator begin()
  36.     {
  37.         return files.begin();
  38.     }
  39.  
  40.     FileNameArray::iterator end()
  41.     {
  42.         return files.end();
  43.     }
  44.  
  45.     int count() const
  46.     {
  47.         return (int)files.size();
  48.     }
  49.  
  50.     std::string operator[](int index)
  51.     {
  52.         return files[index];
  53.     }
  54.  
  55.     void operator()(const std::string &path, const std::string &pattern)
  56.     {
  57.         WIN32_FIND_DATA wfd;
  58.         HANDLE hf;
  59.         std::string findwhat;
  60.         std::vector<std::string> dir;
  61.  
  62.         findwhat = path + "\\*";  // directory
  63.  
  64.         hf = FindFirstFile(findwhat.c_str(), &wfd);
  65.         while (hf != INVALID_HANDLE_VALUE)
  66.         {
  67.             if (wfd.cFileName[0] != '.' && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  68.             {
  69.                 std::string found;
  70.  
  71.                 found = path + "\\" + wfd.cFileName;
  72.                 dir.push_back(found);
  73.             }
  74.  
  75.             if (!FindNextFile(hf, &wfd))
  76.             {
  77.                 FindClose(hf);
  78.                 hf = INVALID_HANDLE_VALUE;
  79.             }
  80.         }
  81.  
  82.         findwhat = path + "\\" + pattern;  // files
  83.  
  84.         hf = FindFirstFile(findwhat.c_str(), &wfd);
  85.         while (hf != INVALID_HANDLE_VALUE)
  86.         {
  87.             if (wfd.cFileName[0] != '.' && !(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  88.             {
  89.                 std::string found;
  90.  
  91.                 found = path + "\\" +  wfd.cFileName;
  92.                 files.push_back(found);
  93.             }
  94.  
  95.             if (!FindNextFile(hf, &wfd))
  96.             {
  97.                 FindClose(hf);
  98.                 hf = INVALID_HANDLE_VALUE;
  99.             }
  100.         }
  101.  
  102.         // continue with directories
  103.         for (std::vector<std::string>::iterator it = dir.begin(); it != dir.end(); ++it)
  104.             this->operator()(*it, pattern);
  105.     }
  106. };
  107.  
  108. ULONG Reflect(ULONG ref, char ch)
  109. {                                 // Used only by Init_CRC32_Table()
  110.  
  111.     ULONG value(0);
  112.  
  113.     // Swap bit 0 for bit 7
  114.     // bit 1 for bit 6, etc.
  115.     for(int i = 1; i < (ch + 1); i++)
  116.     {
  117.         if(ref & 1)
  118.             value |= 1 << (ch - i);
  119.         ref >>= 1;
  120.     }
  121.     return value;
  122. }
  123.  
  124. void InitCrcTable()
  125. {
  126.  
  127.     // 256 values representing ASCII character codes.
  128.     for(int i = 0; i <= 0xFF; i++)
  129.     {
  130.         crc32_table[i]=Reflect(i, 8) << 24;
  131.         for (int j = 0; j < 8; j++)
  132.             crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1 << 31) ? ulPolynomial : 0);
  133.         crc32_table[i] = Reflect(crc32_table[i], 32);
  134.     }
  135.  
  136. }
  137.  
  138. int Get_CRC(unsigned char* buffer, ULONG bufsize)
  139. {
  140.  
  141.     ULONG  crc(0xffffffff);
  142.     int len;
  143.     len = bufsize;
  144.     // Save the text in the buffer.
  145.  
  146.     // Perform the algorithm on each character
  147.     // in the string, using the lookup table values.
  148.  
  149.     for(int i = 0; i < len; i++)
  150.           crc = (crc >> 8) ^ crc32_table[(crc & 0xFF) ^ buffer[i]];
  151.    
  152.  
  153.     // Exclusive OR the result with the beginning value.
  154.     return crc^0xffffffff;
  155.  
  156. }
  157.  
  158. long FileSize(FILE *input)
  159. {
  160.  
  161.   long fileSizeBytes;
  162.   fseek(input, 0, SEEK_END);
  163.   fileSizeBytes = ftell(input);
  164.   fseek(input, 0, SEEK_SET);
  165.  
  166.   return fileSizeBytes;
  167.  
  168. }
  169.  
  170.  
  171. UINT32 getCRCFromFile(string path)
  172. {
  173.  
  174.   FILE *fs = fopen(path.c_str(), "rb");   //open file for reading
  175.  
  176.   UINT32 crc;
  177.   long bufsize = FileSize(fs), result;
  178.   unsigned char *buffer = new unsigned char[bufsize];
  179.  
  180.   if(buffer == NULL)
  181.   {
  182.     printf("\nError out of memory\n");
  183.     return 0;
  184.  
  185.   }
  186.  
  187.    // copy the file into the buffer:
  188.   result = fread (buffer,1,bufsize,fs);
  189.   fclose(fs);
  190.  
  191.   if(result != bufsize)
  192.   {
  193.     printf("\nError reading file %s\n", path.c_str());
  194.     return 0;
  195.   }
  196.  
  197.  
  198.   InitCrcTable();
  199.   crc = Get_CRC(buffer, bufsize);
  200.   delete [] buffer;
  201.  
  202.   return crc;
  203. }
  204.  
  205. // End copy paste code functions
  206.  
  207. #define TEST_T // define when generating header, undef when patch.lst
  208.  
  209. int main(void)
  210. {
  211. #ifdef TEST_T
  212.     SearchFilea sfa;
  213.  
  214.     sfa("C:\\Users\\Harry_\\Desktop\\Update", "*.*");
  215.  
  216.     for (int i = 0; i != sfa.count(); ++i)
  217.     {
  218.         UINT32 crc = getCRCFromFile(sfa[i].c_str());
  219.  
  220.         string path(sfa[i].c_str());
  221.         string filename;
  222.  
  223.         size_t pos = path.find_last_of("\\");
  224.         if(pos != string::npos)
  225.             filename.assign(path.begin() + pos + 1, path.end());
  226.         else
  227.             filename = path;
  228.  
  229.         ofstream OutFile;
  230.  
  231.         UpdateFileHeader test;
  232.         test.HdrSize = 0x0018;
  233.         test.NewFileSize = 0;
  234.         test.OldFileSize = 0;
  235.         test.override = 0x01;
  236.         test.crc32 = crc;
  237.         test.unknown6 = 0;
  238.         test.unknown7 = 0;
  239.         test.version = 0x04;
  240.  
  241.         ifstream file(sfa[i].c_str(), ios::in | ios::binary | ios::ate);
  242.         ifstream::pos_type fileSize;
  243.         char* fileContents;
  244.         if(file.is_open())
  245.         {
  246.             fileSize = file.tellg();
  247.             test.NewFileSize = fileSize;
  248.             test.OldFileSize = fileSize;
  249.             fileContents = new char[fileSize];
  250.             file.seekg(0, ios::beg);
  251.             if(!file.read(fileContents, fileSize))
  252.             {
  253.                 printf("Failed to read.\n");
  254.             }
  255.             file.close();
  256.             OutFile.open(sfa[i].c_str(), ios::binary);
  257.  
  258.             OutFile.write(reinterpret_cast<const char*>(&test), sizeof(UpdateFileHeader));
  259.  
  260.             //const char* output = reinterpret_cast<const char*>(data);
  261.  
  262.             //OutFile.write(output, 24);
  263.             OutFile.write(fileContents, fileSize);
  264.  
  265.             delete[] fileContents;
  266.         }
  267.  
  268.         OutFile.close();
  269.     }
  270.  
  271. #else
  272.     SearchFilea sfa;
  273.  
  274.     sfa("C:\\Users\\Harry_\\Desktop\\Update", "*.*");
  275.  
  276.     ofstream OutFile;
  277.     OutFile.open("C:\\Users\\Harry_\\Desktop\\patch.lst", ios::binary);
  278.     for (int i = 0; i != sfa.count(); ++i)
  279.     {
  280.         std::string filename = "";
  281.         std::string path = sfa[i];
  282.         size_t pos = path.find_last_of("Desktop\\Update\\\\");
  283.         if(pos != string::npos)
  284.             filename.assign(path.begin() + 31, path.end());
  285.         else
  286.             filename = path;
  287.         std::string output = filename; //               path\name
  288.         output = output + ";base\\"; //             ;base
  289.         output = output + filename + ";base\n"; //  \path\filename
  290.         OutFile.write(output.c_str(), output.length());
  291.     }
  292.     OutFile.close();
  293. #endif
  294.  
  295.     return 0;
  296. }
Add Comment
Please, Sign In to add comment