int main() {
    BinaryCode* bin = new BinaryCode();
    string p = "22111";
    auto ret = bin->decode(p);
    cout << ret[0] <<": " << ret[1]<< endl;
    delete bin;
    return 0;
}
示例#2
0
bool KawigiEdit_RunTest(int testNum, string p0, bool hasAnswer, vector <string> p1) {
	cout << "Test " << testNum << ": [" << "\"" << p0 << "\"";
	cout << "]" << endl;
	BinaryCode *obj;
	vector <string> answer;
	obj = new BinaryCode();
	clock_t startTime = clock();
	answer = obj->decode(p0);
	clock_t endTime = clock();
	delete obj;
	bool res;
	res = true;
	cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
	if (hasAnswer) {
		cout << "Desired answer:" << endl;
		cout << "\t" << "{";
		for (int i = 0; int(p1.size()) > i; ++i) {
			if (i > 0) {
				cout << ",";
			}
			cout << "\"" << p1[i] << "\"";
		}
		cout << "}" << endl;
	}
	cout << "Your answer:" << endl;
	cout << "\t" << "{";
	for (int i = 0; int(answer.size()) > i; ++i) {
		if (i > 0) {
			cout << ",";
		}
		cout << "\"" << answer[i] << "\"";
	}
	cout << "}" << endl;
	if (hasAnswer) {
		if (answer.size() != p1.size()) {
			res = false;
		} else {
			for (int i = 0; int(answer.size()) > i; ++i) {
				if (answer[i] != p1[i]) {
					res = false;
				}
			}
		}
	}
	if (!res) {
		cout << "DOESN'T MATCH!!!!" << endl;
	} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
		cout << "FAIL the timeout" << endl;
		res = false;
	} else if (hasAnswer) {
		cout << "Match :-)" << endl;
	} else {
		cout << "OK, but is it right?" << endl;
	}
	cout << "" << endl;
	return res;
}
示例#3
0
int AbstractNumberModel<T>::decompressBeforeAfter(BinaryCode& code, string& beforeAfter) const{
    if(code.finish())
        return -1;
    bool mark = code.getFirstBitMoveToNext();
    if(mark){
        return dictModelPtr->decompressWord(code, beforeAfter);
    } else{
        beforeAfter = "";
        return 0;
    }
}
int SRM_144_Div2_550()
{
	BinaryCode binary;
	vector<string> result = binary.decode("1");
	cout << result[0] << endl;
	cout << result[1] << endl;

	int test;
	cin >> test;

	return 0;
}
int SRM_144_Div1_300()
{
	BinaryCode code;
	vector<string> results = code.decode("123210122");
	cout << results[0] << endl;
	cout << results[1] << endl;

	int test = 0;
	cin >> test;

	return 0;
}
示例#6
0
int GammaEncoder::decode(BinaryCode& code, unsigned int& num) const{
    unsigned int len;
    int ret = unary.decode(code, len);
    if(ret == -1)
        return -1;

    unsigned int remaining = len - 1;
    num = 1;
    if(code.remainSize() < remaining)
        return -1;
    for(unsigned int i = 0; i < remaining; i++){
        num = (num << 1) + code.getFirstBitMoveToNext();
    }
    return 0;
}
示例#7
0
PEAddingMethods<Registers_x86>::ErrorCode PEAddingMethods<Registers_x86>::injectEpCode(QList<uint64_t> &epMethods)
{
    typedef Registers_x86 Register;

    PEFile *pe = dynamic_cast<PEFile*>(file);
    if(!pe)
        return ErrorCode::BinaryFileNoPe;

    BinaryCode<Register> code;

    // Wywołanie każdej z metod
    foreach(uint64_t offset, epMethods)
    {
        code.append(CodeDefines<Register>::movValueToReg(offset, Register::EAX), true);
        code.append(CodeDefines<Register>::callReg(Register::EAX));
    }
bool do_test(string message, vector<string> __expected) {
    time_t startClock = clock();
    BinaryCode *instance = new BinaryCode();
    vector<string> __result = instance->decode(message);
    double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
    delete instance;

    if (__result == __expected) {
        cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
        return true;
    }
    else {
        cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
        cout << "           Expected: " << to_string(__expected) << endl;
        cout << "           Received: " << to_string(__result) << endl;
        return false;
    }
}
示例#9
0
int main(){
	BinaryCode B;
	vector<string> V = B.decode("123210122");
	cout << V[0]<< endl<< V[1]<< endl;
	V = B.decode("11");
	cout << V[0]<< endl<< V[1]<< endl;
	V = B.decode("22111");
	cout << V[0]<< endl<< V[1]<< endl;
	V = B.decode("123210120");
	cout << V[0]<< endl<< V[1]<< endl;
	V = B.decode("3");
	cout << V[0]<< endl<< V[1]<< endl;
	V = B.decode("12221112222221112221111111112221111");
	cout << V[0]<< endl<< V[1]<< endl;
	return 0;

}
示例#10
0
typename PEAddingMethods<Register>::ErrorCode PEAddingMethods<Register>::generateCode
(Wrapper<Register> *w, uint64_t &codePtr, bool isTlsCallback)
{
    ErrorCode ec;
    PEFile *pe = dynamic_cast<PEFile*>(DAddingMethods<Register>::file);
    if(!pe)
        return ErrorCode::BinaryFileNoPe;

    if(!w)
        return ErrorCode::NullWrapper;

    uint64_t action = 0;
    uint64_t thread = 0;

    // Generowanie kodu dla akcji.
    if(w->detect_handler)
    {
        ec = generateCode(w->detect_handler, action);
        if(ec != ErrorCode::Success)
            return ec;
    }

    // Generowanie kodu dla funkcji wątku.
     ThreadWrapper<Register> *tw = dynamic_cast<ThreadWrapper<Register>*>(w);

    if(tw && tw->thread_actions.empty())
        return ErrorCode::NoThreadAction;

    if(tw && !tw->thread_actions.empty())
    {
        ec = generateThreadCode(tw->thread_actions, thread, tw->sleep_time);
        if(ec != ErrorCode::Success) return ec;
    }

    // Generowanie kodu
    BinaryCode<Register> code;

    // Tworzenie ramki stosu
    code.append(CodeDefines<Register>::startFunc);

    std::list<Register> rts = w->used_regs.toStdList();
    bool align = false;

    // Zachowywanie rejestrów
    for(auto it = rts.begin(); it != rts.end(); ++it)
    {
        if(CodeDefines<Register>::externalRegs.contains(*it))
        {
            code.append(CodeDefines<Register>::saveRegister(*it));
            align = !align;
        }
    }

    // Wyrównanie do 16 w przypadku x64
    if(std::is_same<Register, Registers_x64>::value && align)
        code.append(CodeDefines<Register>::reserveStackSpace(CodeDefines<Register>::align16Size));

    // Ładowanie parametrów
    if(!w->dynamic_params.empty())
    {
        DJsonParser parser(DSettings::getSettings().getDescriptionsPath<Register>());
        Wrapper<Register> *func_wrap =
                parser.loadInjectDescription<Register>(windowsApiLoadingFunction);
        if(!func_wrap)
            return ErrorCode::ErrorLoadingFunctions;

        uint64_t get_functions = 0;
        ec = generateCode(func_wrap, get_functions);
        delete func_wrap;

        if(ec != ErrorCode::Success)
            return ec;

        ec = generateParametersLoadingCode(code, get_functions, w->dynamic_params, thread);
        if(ec != ErrorCode::Success)
            return ec;
    }

    // Doklejanie właściwego kodu
    QByteArray binCode;
    ec = compileCode(w->code, binCode);
    if(ec != ErrorCode::Success)
        return ec;

    code.append(binCode);

    // Handler
    if(action)
    {
        Register cond = w->ret;
        int act_idx = CodeDefines<Register>::internalRegs.indexOf(cond);
        Register act = act_idx == -1 ? CodeDefines<Register>::internalRegs[0] :
                CodeDefines<Register>::internalRegs[(act_idx + 1) % CodeDefines<Register>::internalRegs.length()];

        generateActionConditionCode(code, action, cond, act);
    }

    // Wyrównanie do 16 w przypadku x64
    if(std::is_same<Register, Registers_x64>::value && align)
        code.append(CodeDefines<Register>::clearStackSpace(CodeDefines<Register>::align16Size));

    // Przywracanie rejestrów
    for(auto it = rts.rbegin(); it != rts.rend(); ++it)
    {
        if(CodeDefines<Register>::externalRegs.contains(*it))
            code.append(CodeDefines<Register>::restoreRegister(*it));
    }

    // Niszczenie ramki stosu i ret
    code.append(CodeDefines<Register>::endFunc);
    if(isTlsCallback && !std::is_same<Register, Registers_x64>::value)
        code.append(CodeDefines<Register>::retN(3 * CodeDefines<Register>::stackCellSize));
    else
        code.append(CodeDefines<Register>::ret);

    codePtr = pe->injectUniqueData(code, codePointers, relocations);

    return codePtr == 0 ? ErrorCode::PeOperationFailed : ErrorCode::Success;
}