stoneharry

Untitled

Apr 6th, 2013
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.65 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 SearchFile
  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. int main(void)
  208. {
  209.     SearchFile sf;
  210.  
  211.     sf("C:\\Users\\Harry\\Desktop\\test3\\", "*.*");
  212.  
  213.     for (int i = 0; i != sf.count(); ++i)
  214.     {
  215.         UINT32 crc = getCRCFromFile(sf[i].c_str());
  216.  
  217.         string path(sf[i].c_str());
  218.         string filename;
  219.  
  220.         size_t pos = path.find_last_of("\\");
  221.         if(pos != string::npos)
  222.             filename.assign(path.begin() + pos + 1, path.end());
  223.         else
  224.             filename = path;
  225.  
  226.         ofstream OutFile;
  227.         OutFile.open("C:\\Users\\Harry\\Desktop\\test5\\" + filename, ios::binary);
  228.  
  229.         UpdateFileHeader test;
  230.         test.HdrSize = 0x0018;
  231.         test.NewFileSize = 0;
  232.         test.OldFileSize = 0;
  233.         test.override = 0x01;
  234.         test.crc32 = crc;
  235.         test.unknown6 = 0;
  236.         test.unknown7 = 0;
  237.         test.version = 0x04;
  238.  
  239.         ifstream file(sf[i].c_str(), ios::in | ios::binary | ios::ate);
  240.         ifstream::pos_type fileSize;
  241.         char* fileContents;
  242.         if(file.is_open())
  243.         {
  244.             fileSize = file.tellg();
  245.             test.NewFileSize = fileSize;
  246.             test.OldFileSize = fileSize;
  247.             fileContents = new char[fileSize];
  248.             file.seekg(0, ios::beg);
  249.             if(!file.read(fileContents, fileSize))
  250.             {
  251.                 printf("Failed to read.\n");
  252.             }
  253.             file.close();
  254.  
  255.             OutFile.write(reinterpret_cast<const char*>(&test.HdrSize), sizeof(UINT16));
  256.             OutFile.write(reinterpret_cast<const char*>(&test.version), sizeof(UINT8));
  257.             OutFile.write(reinterpret_cast<const char*>(&test.override), sizeof(UINT8));
  258.             OutFile.write(reinterpret_cast<const char*>(&test.crc32), sizeof(UINT32));
  259.             OutFile.write(reinterpret_cast<const char*>(&test.OldFileSize), sizeof(UINT32));
  260.             OutFile.write(reinterpret_cast<const char*>(&test.NewFileSize), sizeof(UINT32));
  261.             OutFile.write(reinterpret_cast<const char*>(&test.crc32), sizeof(UINT32));
  262.             OutFile.write(reinterpret_cast<const char*>(&test.crc32), sizeof(UINT32));
  263.  
  264.             //const char* output = reinterpret_cast<const char*>(data);
  265.  
  266.             //OutFile.write(output, 24);
  267.             OutFile.write(fileContents, fileSize);
  268.  
  269.             delete[] fileContents;
  270.         }
  271.  
  272.         OutFile.close();
  273.     }
  274.  
  275.     return 0;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment