Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <thread>
- #include <string>
- #include <fstream>
- using namespace std;
- typedef vector<char>::iterator it;
- vector<char>readFile(const char* fileName){
- int NumberOfBits;
- vector<char> vecReadBits;
- ifstream file(fileName, ios::in | ios::binary | ios::ate);
- if(file.is_open()){
- NumberOfBits = file.tellg();
- char* MemBlock = new char[NumberOfBits];
- file.seekg(0, ios::beg);
- file.read(MemBlock, NumberOfBits);
- file.close();
- vecReadBits.resize(NumberOfBits-4);
- for(int i = 0; i<NumberOfBits-4; i++){
- vecReadBits.push_back(MemBlock[i]);
- }
- cout<<"This file [" << fileName <<"] contains "<< NumberOfBits-4<<" bits."<<endl;
- delete [] MemBlock;
- cout << "SUCCESSFULLY DONE - readFile - " << fileName << endl;
- }else cout<<"ERROR - readFile."<<endl;
- return vecReadBits;
- }
- void writeInFile(string fileName, vector<char> vec){
- it vecIt=vec.begin();
- char* MemBlock = new char[vec.size()];
- ofstream file(fileName, ios::out | ios::binary);
- for(int i=0; vecIt != vec.end(); vecIt++, i++)
- MemBlock[i] = *vecIt;
- file.write(MemBlock,vec.size());
- file.close();
- cout << "SUCCESSFULLY DONE - writeInFile - " << fileName << endl;
- }
- void XOR(it inputBegin, it inputEnd, it keyBegin, it keyEnd, it outBegin){
- it inputIt = inputBegin;
- it keyIt = keyBegin;
- it outIt = outBegin;
- for(;inputIt!=inputEnd; inputIt++, keyIt++, outIt++){
- if(keyIt >= keyEnd)
- keyIt = keyBegin;
- *outIt = *inputIt ^ *keyIt;
- }
- }
- int main(int argc, char* argv[])
- {
- vector<char> vecInput = readFile(argv[1]);
- vector<char> vecKey = readFile(argv[2]);
- vector<char> vecOut(vecInput.size());
- int threadNum = stoi(argv[4]);
- int threadSize = vecInput.size() / threadNum;
- thread threadList[threadNum];
- int i;
- bool TheLastThreadIsDifferent = (vecInput.size() % threadNum != 0);
- int NumberOfForLoops = threadNum;
- if(TheLastThreadIsDifferent) NumberOfForLoops--;
- for(i=0; i<NumberOfForLoops; i++){
- // vector<char>::iterator inItBegin = vecInput.begin() + i*threadSize;
- // vector<char>::iterator inItEnd = inItBegin + i*threadSize;
- // vector<char>::iterator keyItBegin = vecKey.begin() + i*threadSize;
- // vector<char>::iterator outItBegin = vecOut.begin() + i*threadSize;
- // if(keyItBegin >= vecKey.end())
- // keyItBegin = vecKey.begin();
- threadList[i] = thread(XOR, vecInput.begin()+ i*threadSize,
- vecInput.begin()+ (i+1)*threadSize,
- vecKey.begin() + i*threadSize,
- vecKey.begin() + (i+1)*threadSize,
- vecOut.begin() + i*threadSize);
- }
- if(TheLastThreadIsDifferent){
- i++;
- threadList[i] = thread(XOR, vecInput.begin()+ i*threadSize,
- vecInput.end(),
- vecKey.begin() + i*threadSize,
- vecKey.begin() + (i+1)*threadSize,
- vecOut.begin() + i*threadSize);
- }
- for(int i=0; i<threadNum; i++)
- threadList[i].join();
- cout << "SUCCESSFULLY DONE - XOR with threads" << endl;
- writeInFile(argv[3],vecOut);
- cout << "SUCCSSFULLY DONE - THE WHOLE PROGRAM" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement