Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <thread>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8. typedef vector<char>::iterator it;
  9.  
  10. vector<char>readFile(const char* fileName){
  11.     int NumberOfBits;
  12.     vector<char> vecReadBits;
  13.     ifstream file(fileName, ios::in | ios::binary | ios::ate);
  14.     if(file.is_open()){
  15.         NumberOfBits = file.tellg();
  16.         char* MemBlock = new char[NumberOfBits];
  17.         file.seekg(0, ios::beg);
  18.         file.read(MemBlock, NumberOfBits);
  19.         file.close();
  20.         vecReadBits.resize(NumberOfBits-4);
  21.         for(int i = 0; i<NumberOfBits-4; i++){
  22.             vecReadBits.push_back(MemBlock[i]);
  23.         }
  24.         cout<<"This file [" << fileName <<"] contains "<< NumberOfBits-4<<" bits."<<endl;
  25.         delete [] MemBlock;
  26.         cout << "SUCCESSFULLY DONE - readFile - " << fileName << endl;
  27.     }else cout<<"ERROR - readFile."<<endl;
  28.     return vecReadBits;
  29. }
  30.  
  31. void writeInFile(string fileName, vector<char> vec){
  32.     it vecIt=vec.begin();
  33.     char* MemBlock = new char[vec.size()];
  34.     ofstream file(fileName, ios::out | ios::binary);
  35.     for(int i=0; vecIt != vec.end(); vecIt++, i++)
  36.         MemBlock[i] = *vecIt;
  37.     file.write(MemBlock,vec.size());
  38.        file.close();
  39.         cout << "SUCCESSFULLY DONE - writeInFile - " << fileName << endl;
  40. }
  41.  
  42. void XOR(it inputBegin, it inputEnd, it keyBegin, it keyEnd, it outBegin){
  43.  
  44.     it inputIt  = inputBegin;
  45.     it keyIt    = keyBegin;
  46.     it outIt    = outBegin;
  47.  
  48.     for(;inputIt!=inputEnd; inputIt++, keyIt++, outIt++){
  49.         if(keyIt >= keyEnd)
  50.             keyIt = keyBegin;
  51.         *outIt = *inputIt ^ *keyIt;
  52.     }
  53. }
  54.  
  55. int main(int argc, char* argv[])
  56. {
  57.     vector<char> vecInput   = readFile(argv[1]);
  58.     vector<char> vecKey     = readFile(argv[2]);
  59.     vector<char> vecOut(vecInput.size());
  60.  
  61.     int threadNum  = stoi(argv[4]);
  62.     int threadSize = vecInput.size() / threadNum;
  63.     thread threadList[threadNum];
  64.  
  65.     int i;
  66.     bool TheLastThreadIsDifferent = (vecInput.size() % threadNum != 0);
  67.     int NumberOfForLoops = threadNum;
  68.     if(TheLastThreadIsDifferent) NumberOfForLoops--;
  69.     for(i=0; i<NumberOfForLoops; i++){
  70.  
  71.        // vector<char>::iterator  inItBegin  = vecInput.begin() + i*threadSize;
  72.        // vector<char>::iterator  inItEnd    = inItBegin        + i*threadSize;
  73.        // vector<char>::iterator  keyItBegin = vecKey.begin()   + i*threadSize;
  74.        // vector<char>::iterator  outItBegin = vecOut.begin()   + i*threadSize;
  75.  
  76. //         if(keyItBegin >= vecKey.end())
  77.   //          keyItBegin = vecKey.begin();
  78.  
  79.         threadList[i] = thread(XOR, vecInput.begin()+ i*threadSize,
  80.                                     vecInput.begin()+ (i+1)*threadSize,
  81.                                     vecKey.begin()  + i*threadSize,
  82.                                     vecKey.begin()  + (i+1)*threadSize,
  83.                                     vecOut.begin()  + i*threadSize);
  84.     }
  85.     if(TheLastThreadIsDifferent){
  86.         i++;
  87.         threadList[i] = thread(XOR, vecInput.begin()+ i*threadSize,
  88.                                     vecInput.end(),
  89.                                     vecKey.begin()  + i*threadSize,
  90.                                     vecKey.begin()  + (i+1)*threadSize,
  91.                                     vecOut.begin()  + i*threadSize);
  92.     }
  93.  
  94.     for(int i=0; i<threadNum; i++)
  95.         threadList[i].join();
  96.  
  97.     cout << "SUCCESSFULLY DONE - XOR with threads" << endl;
  98.     writeInFile(argv[3],vecOut);
  99.     cout << "SUCCSSFULLY DONE - THE WHOLE PROGRAM" << endl;
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement