Beispiel #1
0
void ParityUtils::init(void) {
	str_par_map_.insert(std::make_pair("Even", Parity(Parity::even)));
	str_par_map_.insert(std::make_pair("Odd", Parity(Parity::odd)));
	str_par_map_.insert(std::make_pair("None", Parity(Parity::none)));

	typedef StringParityMap::const_iterator CIter;
	CIter iter = str_par_map_.begin();
	CIter end = str_par_map_.end();
	while (iter != end) {
		par_str_map_.insert(std::make_pair(iter->second, iter->first));
		++iter;
	}
}
Beispiel #2
0
int Or::Execute(Processor* proc) {

	Operand* dst = mOperands[Operand::DST];
	Operand* src = mOperands[Operand::SRC];

	if(!dst || !src) {
		return INVALID_ARGS;
	}

	unsigned int dstVal = dst->GetValue();
	unsigned int srcVal = src->GetValue();
	unsigned int newVal = dstVal | srcVal;

	unsigned int sign = (dst->GetBitmask() == 0xFF) ? 0x80 : 0x8000;

	proc->SetFlag(FLAGS_OF, 0);
	proc->SetFlag(FLAGS_CF, 0);
	proc->SetFlag(FLAGS_SF, newVal >= sign);
	proc->SetFlag(FLAGS_ZF, newVal == 0x00);
	proc->SetFlag(FLAGS_PF, Parity(newVal));

	dst->SetValue(newVal);

	return 0;
}
Beispiel #3
0
int Adc::Execute(Processor* proc) {

	Operand* dst = mOperands[Operand::DST];
	Operand* src = mOperands[Operand::SRC];

	if(!dst || !src) {
		return INVALID_ARGS;
	}

	unsigned int dstVal = dst->GetValue();
	unsigned int srcVal = src->GetValue();
	unsigned int sign = dst->GetBitmask() == 0xFF ? 0x80 : 0x8000;
	unsigned int newVal = dstVal + srcVal + (proc->GetFlag(FLAGS_CF) ? 1 : 0);
	proc->SetFlag(FLAGS_CF, newVal > dst->GetBitmask());
	newVal &= dst->GetBitmask();

	proc->SetFlag(FLAGS_OF, OverflowAdd(dstVal, srcVal, sign == 0x80 ? 1 : 2));
	proc->SetFlag(FLAGS_SF, newVal >= sign);
	proc->SetFlag(FLAGS_ZF, newVal == 0x00);
	proc->SetFlag(FLAGS_AF, AdjustAdd(dstVal, srcVal));
	proc->SetFlag(FLAGS_PF, Parity(newVal));

	dst->SetValue(newVal);

	return 0;
}
Beispiel #4
0
Parity ParityUtils::getParity(const std::string & pari) const {
	StringParityMap::const_iterator iter = str_par_map_.find(pari);
	if (iter != str_par_map_.end()) {
		return iter->second;
	}
	return Parity(Parity::none);

}
Beispiel #5
0
	int ZWS_A(const uint64_t P, const uint64_t O, uint64_t& NodeCounter, const int alpha, const int empties)
	{
		assert((P & O) == 0);
		assert(-64 <= alpha); assert(alpha <= 64);
		assert(0 <= empties); assert(empties <= 60); assert(empties == Empties(P, O));

		if (empties <= 4) {
			if (empties == 4) return ZWS_4(P, O, NodeCounter, alpha);
			if (empties == 3) return ZWS_3(P, O, NodeCounter, alpha);
			if (empties == 2) return ZWS_2(P, O, NodeCounter, alpha);
			if (empties == 1) return ZWS_1(P, O, NodeCounter, alpha);
			if (empties == 0) return Eval_0(P, NodeCounter);
		}

		int score;
		int bestscore = -64;
		unsigned long Move;
		uint64_t flipped;
		uint64_t BitBoardPossible = PossibleMoves(P, O);
		NodeCounter++;

		if (!BitBoardPossible){
			if (HasMoves(O, P))
				return -ZWS_A(O, P, NodeCounter, -alpha - 1, empties);
			else
				return EvalGameOver(P, empties);
		}

		if (StabilityCutoff_ZWS(P, O, alpha, score)) return score;
		
		const uint64_t BBParity = quadrant[Parity(P, O)];
		uint64_t BBTmp;

		BBTmp = BitBoardPossible &  BBParity;
		while (BBTmp)
		{
			Move = BitScanLSB(BBTmp);
			RemoveLSB(BBTmp);
			flipped = flip(P, O, Move);
			score = -ZWS_A(O ^ flipped, P ^ (1ULL << Move) ^ flipped, NodeCounter, -alpha-1, empties-1);
			if (score > alpha) return score;
			if (score > bestscore) bestscore = score;
		}

		BBTmp = BitBoardPossible & ~BBParity;
		while (BBTmp)
		{
			Move = BitScanLSB(BBTmp);
			RemoveLSB(BBTmp);
			flipped = flip(P, O, Move);
			score = -ZWS_A(O ^ flipped, P ^ (1ULL << Move) ^ flipped, NodeCounter, -alpha-1, empties-1);
			if (score > alpha) return score;
			if (score > bestscore) bestscore = score;
		}
		return bestscore;
	}
Beispiel #6
0
void SetFlags(byte Datum) {
	Flag3=(Datum&0x08)!=0;
	Flag5=(Datum&0x20)!=0;
	if(Datum==0) FlagZ=1; else FlagZ=0;
	FlagNZ=!FlagZ;
	FlagP=!SignBit(Datum);
        FlagM=!FlagP;
	FlagPE=Parity(Datum);
	FlagPO=!FlagPE;
}
Beispiel #7
0
static void proc_block160(uint32_t *hash, const byte_t *input)
{
	uint32_t a, b, c, d, e;
	uint32_t wt[80];

	expand_w160(wt, input);
	a = hash[0];
	b = hash[1];
	c = hash[2];
	d = hash[3];
	e = hash[4];

	sha1_core(a, b, c, d, e, k160[0], wt, Ch<uint32_t>());
	sha1_core(a, b, c, d, e, k160[1], wt + 20, Parity());
	sha1_core(a, b, c, d, e, k160[2], wt + 40, Maj<uint32_t>());
	sha1_core(a, b, c, d, e, k160[3], wt + 60, Parity());

	hash[0] += a;
	hash[1] += b;
	hash[2] += c;
	hash[3] += d;
	hash[4] += e;
}
Beispiel #8
0
int Test::Execute(Processor* proc) {

	if(mOperands[Operand::SRC] == 0 || mOperands[Operand::DST] == 0)
		return INVALID_ARGS;

	unsigned int val = mOperands[Operand::SRC]->GetValue() & mOperands[Operand::DST]->GetValue();
	unsigned int sign = mOperands[Operand::DST]->GetBitmask() == 0xFF ? 0x80 : 0x8000;

	proc->SetFlag(FLAGS_CF, false);
	proc->SetFlag(FLAGS_OF, false);
	proc->SetFlag(FLAGS_PF, Parity(val));
	proc->SetFlag(FLAGS_ZF, val == 0);
	proc->SetFlag(FLAGS_SF, val >= sign);

	return 0;
}
Beispiel #9
0
int Scas::Execute(Processor* proc) {
	unsigned int ax = 0;
	unsigned int mem_di = 0;
	unsigned int sign = 0;
	unsigned int newVal = 0;
	unsigned int bitmask = 0;

	switch(mOpcode) {
	case SCASB:
		ax = proc->GetRegister(REG_AL);
		mem_di = proc->GetMemory(proc->GetRegister(REG_DI), 1);
		sign = 0x80;
		bitmask = 0xFF;
		break;
	case SCASW:
		ax = proc->GetRegister(REG_AX);
		mem_di = proc->GetMemory(proc->GetRegister(REG_DI), 2);
		sign = 0x8000;
		bitmask =0xFFFF;
		break;
	default:
		return INVALID_ARGS;
	}

	newVal = ax - mem_di;

	proc->SetFlag(FLAGS_CF, newVal > ax);
	newVal &= bitmask;

	proc->SetFlag(FLAGS_OF, OverflowSub(ax, mem_di, sign == 0x80 ? 1 : 2));
	proc->SetFlag(FLAGS_SF, newVal >= sign);
	proc->SetFlag(FLAGS_ZF, newVal == 0x00);
	proc->SetFlag(FLAGS_AF, AdjustSub(ax, mem_di));
	proc->SetFlag(FLAGS_PF, Parity(newVal));

	if(proc->GetFlag(FLAGS_DF))
		proc->SetRegister(REG_DI, proc->GetRegister(REG_DI) - (sign == 0x80 ? 1 : 2));
	else
		proc->SetRegister(REG_DI, proc->GetRegister(REG_DI) + (sign == 0x80 ? 1 : 2));

	return 0;
}
Beispiel #10
0
int Xor::Execute(Processor* proc) {
	Operand* dst = mOperands[Operand::DST];
	Operand* src = mOperands[Operand::SRC];

	if(dst == 0 || src == 0)
		return -1;

	unsigned int val = dst->GetValue() ^ src->GetValue();

	proc->SetFlag(FLAGS_OF, 0);
	proc->SetFlag(FLAGS_CF, 0);

	proc->SetFlag(FLAGS_ZF, val == 0);
	proc->SetFlag(FLAGS_PF, Parity(val));
	proc->SetFlag(FLAGS_SF, val >= (dst->GetBitmask() == 0xFF ? 0x80 : 0x8000));

	dst->SetValue(val);

	return 0;
}
Beispiel #11
0
int Neg::Execute(Processor* proc) {
	Operand* dst = mOperands[Operand::DST];

	if(!dst) {
		return INVALID_ARGS;
	}
	unsigned int dstVal = dst->GetValue();
	unsigned int sign = dst->GetBitmask() == 0xFF ? 0x80 : 0x8000;
	proc->SetFlag(FLAGS_CF, dstVal != 0);
	proc->SetFlag(FLAGS_OF, dstVal == 0x80); //only overflow is -128 -> -128
	
	dst->SetValue((~dstVal + 1) & dst->GetBitmask());
	dstVal = dst->GetValue();

	proc->SetFlag(FLAGS_SF, dstVal >= sign);
	proc->SetFlag(FLAGS_ZF, dstVal == 0x00);
	proc->SetFlag(FLAGS_PF, Parity(dstVal));
	proc->SetFlag(FLAGS_AF, AdjustSub(dstVal, dstVal*2));

	return 0;
}
Beispiel #12
0
unsigned int Cmp::compare(Processor* proc, Operand* dst, Operand* src) {
	if(!dst || !src) {
		return 0xFFFFFFFF;
	}

	unsigned int dstVal = dst->GetValue();
	unsigned int srcVal = src->GetValue();
	unsigned int newVal = dstVal - srcVal;
	unsigned int sign = dst->GetBitmask() == 0xFF ? 0x80 : 0x8000;

	proc->SetFlag(FLAGS_CF, newVal > dstVal);
	newVal &= dst->GetBitmask();

	proc->SetFlag(FLAGS_OF, OverflowSub(dstVal, srcVal, sign == 0x80 ? 1 : 2));
	proc->SetFlag(FLAGS_SF, newVal >= sign);
	proc->SetFlag(FLAGS_ZF, newVal == 0x00);
	proc->SetFlag(FLAGS_AF, AdjustSub(dstVal, srcVal));

	proc->SetFlag(FLAGS_PF, Parity(newVal));

	return newVal;
}
Beispiel #13
0
static void SHA1_HashBlock(SHA1_CONTEXT *p)
{
    int t, j;
    UINT32 W[80], a, b, c, d, e, T;

    // Prepare Message Schedule, {W sub t}.
    //
    for (t = 0, j = 0; t <= 15; t++, j += 4)
    {
        W[t] = (p->block[j  ] << 24)
             | (p->block[j+1] << 16)
             | (p->block[j+2] <<  8)
             | (p->block[j+3]      );
    }
    for (t = 16; t <= 79; t++)
    {
        W[t] = ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
    }

    a = p->H[0];
    b = p->H[1];
    c = p->H[2];
    d = p->H[3];
    e = p->H[4];

    for (t =  0; t <= 19; t++)
    {
        T = ROTL(a,5) + Ch(b,c,d) + e + 0x5A827999 + W[t];
        e = d;
        d = c;
        c = ROTL(b,30);
        b = a;
        a = T;
    }
    for (t = 20; t <= 39; t++)
    {
        T = ROTL(a,5) + Parity(b,c,d) + e + 0x6ED9EBA1 + W[t];
        e = d;
        d = c;
        c = ROTL(b,30);
        b = a;
        a = T;
    }
    for (t = 40; t <= 59; t++)
    {
        T = ROTL(a,5) + Maj(b,c,d) + e + 0x8F1BBCDC + W[t];
        e = d;
        d = c;
        c = ROTL(b,30);
        b = a;
        a = T;
    }
    for (t = 60; t <= 79; t++)
    {
        T = ROTL(a,5) + Parity(b,c,d) + e + 0xCA62C1D6 + W[t];
        e = d;
        d = c;
        c = ROTL(b,30);
        b = a;
        a = T;
    }

    p->H[0] += a;
    p->H[1] += b;
    p->H[2] += c;
    p->H[3] += d;
    p->H[4] += e;
}
Beispiel #14
0
unsigned int RandomNumberGenerator::GenerateBit()
{
	return Parity(GenerateByte());
}
Beispiel #15
0
/*
========================================================================
Routine Description:
    SHA1 computation for one block (512 bits)

Arguments:
    pSHA_CTX        Pointer to SHA1_CTX_STRUC

Return Value:
    None

Note:
    None
========================================================================
*/
VOID RT_SHA1_Hash (
    IN  SHA1_CTX_STRUC *pSHA_CTX)
{
    uint32_t W_i,t;
    uint32_t W[80];
    uint32_t a,b,c,d,e,T,f_t = 0;

    /* Prepare the message schedule, {W_i}, 0 < t < 15 */
    memmove(W, pSHA_CTX->Block, SHA1_BLOCK_SIZE);
    for (W_i = 0; W_i < 16; W_i++) {
        W[W_i] = cpu2be32(W[W_i]); /* Endian Swap */
    } /* End of for */

    for (W_i = 16; W_i < 80; W_i++) {
        W[W_i] = ROTL32((W[W_i - 3] ^ W[W_i - 8] ^ W[W_i - 14] ^ W[W_i - 16]),1);
    } /* End of for */


    /* SHA256 hash computation */
    /* Initialize the working variables */
    a = pSHA_CTX->HashValue[0];
    b = pSHA_CTX->HashValue[1];
    c = pSHA_CTX->HashValue[2];
    d = pSHA_CTX->HashValue[3];
    e = pSHA_CTX->HashValue[4];

    /* 80 rounds */
    for (t = 0;t < 20;t++) {
        f_t = Ch(b,c,d);
        T = ROTL32(a,5) + f_t + e + SHA1_K[0] + W[t];
        e = d;
        d = c;
        c = ROTL32(b,30);
        b = a;
        a = T;
     } /* End of for */
    for (t = 20;t < 40;t++) {
        f_t = Parity(b,c,d);
        T = ROTL32(a,5) + f_t + e + SHA1_K[1] + W[t];
        e = d;
        d = c;
        c = ROTL32(b,30);
        b = a;
        a = T;
     } /* End of for */
    for (t = 40;t < 60;t++) {
        f_t = Maj(b,c,d);
        T = ROTL32(a,5) + f_t + e + SHA1_K[2] + W[t];
        e = d;
        d = c;
        c = ROTL32(b,30);
        b = a;
        a = T;
     } /* End of for */
    for (t = 60;t < 80;t++) {
        f_t = Parity(b,c,d);
        T = ROTL32(a,5) + f_t + e + SHA1_K[3] + W[t];
        e = d;
        d = c;
        c = ROTL32(b,30);
        b = a;
        a = T;
     } /* End of for */


     /* Compute the i^th intermediate hash value H^(i) */
     pSHA_CTX->HashValue[0] += a;
     pSHA_CTX->HashValue[1] += b;
     pSHA_CTX->HashValue[2] += c;
     pSHA_CTX->HashValue[3] += d;
     pSHA_CTX->HashValue[4] += e;

    memset(pSHA_CTX->Block, 0, SHA1_BLOCK_SIZE);
    pSHA_CTX->BlockLen = 0;
} /* End of RT_SHA1_Hash */
Beispiel #16
0
__bool CSerialPort::open()
{
	char			buf[64];
	DCB				dcb;
	COMMTIMEOUTS	tout;

	if(INVALID_HANDLE_VALUE != m_hFile){
		return __true;
	}
	sprintf(buf, "\\\\.\\COM%d", m_iPortNo);
	m_hFile = CreateFile(
		buf, 
		GENERIC_READ | GENERIC_WRITE,
		0,
		0,
		OPEN_EXISTING,
		FILE_FLAG_OVERLAPPED,
		0
		);
	utils_trace(
		"COM %d opened as 0x%08x\n", 
		m_iPortNo,
		m_hFile
		);
	if(INVALID_HANDLE_VALUE==m_hFile){
		return __false;
	}
	
	get_config_string();
	
	if( !setup_dcb(m_Setting, &dcb) ){
		close();
		utils_trace(
			"COM%d, error in setting string : %s, code %d\n", 
			m_iPortNo,
			m_Setting,
			GetLastError()
			);
		return __false;
	}

	if( !SetCommState(m_hFile, &dcb) ){
		utils_error(
			"COM%d, SetCommState failed with %d.\n",
			m_iPortNo,
			GetLastError()
			);
		close();
	}

	utils_trace(
		"Ok, COM%d setting applied : '%s'\n", 
		m_iPortNo,
		m_Setting
		);

	if( !GetCommTimeouts(m_hFile, &tout) ){
		close();
		utils_trace(
			"COM%d, Error in GetCommTimeouts, Code %d.\n", 
			m_iPortNo,
			GetLastError()
			);
		return __false;
	}

	tout.ReadIntervalTimeout = MAXDWORD;
	tout.ReadTotalTimeoutMultiplier = 0;
	tout.ReadTotalTimeoutConstant = 0;
	tout.WriteTotalTimeoutMultiplier = 10;
	tout.WriteTotalTimeoutConstant = 0;
	if(!SetCommTimeouts(m_hFile, &tout)){
		utils_trace(
			"COM%d, Error in SetCommTimeouts, Code %d.\n", 
			m_iPortNo,
			GetLastError()
			);
		close();
		return __false;
	}

	m_OverlappedEvent = new CEvent(0, TRUE, FALSE);
	if(!m_OverlappedEvent || !m_OverlappedEvent->Handle()){
		utils_trace(
			"COM%d, error %d creating overlapped event.\n",
			m_iPortNo,
			GetLastError()
			);
		close();
		return __false;
	}

	if( !PurgeComm(
		m_hFile, 
		PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR
		)
	){
		utils_error(
			"COM%d, Warning : PurgeComm failed with %d\n",
			m_iPortNo,
			GetLastError()
			);
	}

	COMMCONFIG	cfg;
	DWORD size;
	cfg.dwSize = size = sizeof(cfg);
	if( !GetCommConfig(m_hFile, &cfg, &size) ){
		utils_error(
			"Warning : GetCommConfig failed with %d\n", 
			GetLastError()
			);
	}else{
		utils_trace(
			"COM%d setting is : '%d,%c,%d,%d(%08x)'\n",
			m_iPortNo,
			cfg.dcb.BaudRate,
			Parity(&cfg.dcb),
			cfg.dcb.ByteSize,
			Stopbits(cfg.dcb.StopBits),
			*((DWORD*)&cfg.dcb + 2)
			);
	}

	return __true;
}
void HashSHA1Block(void* hash_block, SHA1_Context* ctx)
{
	unsigned int a,b,c,d,e,T,i;
	unsigned char* block = (unsigned char*)hash_block;
	unsigned int w[0x50];
	a = ctx->h0;
	b = ctx->h1;
	c = ctx->h2;
	d = ctx->h3;
	e = ctx->h4;
	for (i = 0; i < 16; i++) w[i] = BSWAP(*(unsigned int*)(block + i * 4));
	for (i = 16; i < 80; i++) w[i] = ROTL( w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
	
	for (i = 0; i < 20; i++)
	{
		T = ROTL(a,5) + Ch(b,c,d) + e + sha1_constant[0] + w[i];
		e = d;
		d = c;
		c = ROTL(b,30);
		b = a;
		a = T;

	}
	for (i=20;i<40;i++)
	{
		T = ROTL(a,5) + Parity(b,c,d) + e + sha1_constant[1] + w[i];
		e = d;
		d = c;
		c = ROTL(b,30);
		b = a;
		a = T;

	}
	for (i=40;i<60;i++)
	{
		T = ROTL(a,5) + Maj(b,c,d) + e + sha1_constant[2] + w[i];
		e = d;
		d = c;
		c = ROTL(b,30);
		b = a;
		a = T;

	}
	for (i=60;i<80;i++)
	{
		T = ROTL(a,5) + Parity(b,c,d) + e + sha1_constant[3] + w[i];
		e = d;
		d = c;
		c = ROTL(b,30);
		b = a;
		a = T;

	}

	ctx->h0 += a;
	ctx->h1 += b;
	ctx->h2 += c;
	ctx->h3 += d;
	ctx->h4 += e;
	a = b = c = d = e = T = 0;
	memset(w, 0, 0x140);
}