#include <vector>
#include <string>
#include <map>
#include <fstream>
#include <math.h>
#include "Comp.h"
#include "StringOps.h"
extern int UINTSIZE;
Comp::Comp(int iShiftSize) {
shiftSize = iShiftSize;
}
Comp::~Comp() {
clearFuncStore();
}
string Comp::returnName() {
string compStr;
int i;
for(i = compNames.size() - 1; i >= 0; i--) {
compStr.append(compNames[i]);
if(i != 0)
compStr.append("#");
}
return compStr;
}
void Comp::printDefinitions(ofstream & fout) {
int i;
map <string, bool> seen;
map<string, Func *>::iterator frt;
for(i = compNames.size() - 1; i >= 0; i--) {
if(seen.find(compNames[i]) == seen.end()) {
seen.insert(pair<string,bool>(compNames[i], true));
frt = funcStore.find(compNames[i]);
if(frt->second->isOpt())
fout << "OPT ";
fout << compNames[i] << " = ";
frt->second->print(fout);
}
}
}
void Comp::print() {
}
bool Comp::setComposition(const string & inStr) {
int i;
vector <string> cArr;
split(inStr, '#', cArr);
composition.clear();
compNames.clear();
for(i = cArr.size() - 1; i >= 0; i--) {
trim(cArr[i]);
map<string, Func *>::iterator cfunc = funcStore.find(cArr[i]);
if(cfunc == funcStore.end())
return false;
else {
composition.push_back(cfunc->second);
compNames.push_back(cArr[i]);
}
}
return true;
}
void Comp::addFunc(const string & inStr, string name, bool opt) {
trim(name);
funcStore.insert(pair<string, Func *>(name,
new Func(inStr, shiftSize, opt)));
}
void Comp::image(byte *word, int wordLength, byte *output) {
int size = composition.size(), i;
byte temp[wordLength];
copyWord(temp, word, wordLength);
for(i = 0; i < size; i++) {
if(i % 2 == 0) {
composition[i]->image(temp, wordLength, output);
}
else {
composition[i]->image(output, wordLength, temp);
}
}
if(size % 2 == 0)
copyWord(output, temp, wordLength);
}
void Comp::image(unsigned int *word, int wordLength, unsigned int *output,
unsigned int blocks) {
int size = composition.size(), i;
unsigned int temp[blocks];
copyUIntArray(temp, word, blocks);
for(i = 0; i < size; i++) {
if(i % 2 == 0) {
composition[i]->image(temp, wordLength, output, blocks);
}
else {
composition[i]->image(output, wordLength, temp, blocks);
}
}
if(size % 2 == 0)
copyUIntArray(output, temp, blocks);
}
void Comp::clearFuncStore() {
map<string, Func *>::iterator it;
for(it = funcStore.begin(); it != funcStore.end(); it++) {
delete(it->second);
}
}