Пример #1
0
/* =============================================================================
 * fixAfterInsertion
 * =============================================================================
 */
static void
fixAfterInsertion (rbtree_t* s, node_t* x)
{
    STF(x, c, RED);
    while (x != NULL && x != LDNODE(s, root)) {
        node_t* xp = LDNODE(x, p);
        if (LDF(xp, c) != RED) {
            break;
        }
        /* TODO: cache g = ppx = PARENT_OF(PARENT_OF(x)) */
        if (PARENT_OF(x) == LEFT_OF(PARENT_OF(PARENT_OF(x)))) {
            node_t*  y = RIGHT_OF(PARENT_OF(PARENT_OF(x)));
            if (COLOR_OF(y) == RED) {
                SET_COLOR(PARENT_OF(x), BLACK);
                SET_COLOR(y, BLACK);
                SET_COLOR(PARENT_OF(PARENT_OF(x)), RED);
                x = PARENT_OF(PARENT_OF(x));
            } else {
                if (x == RIGHT_OF(PARENT_OF(x))) {
                    x = PARENT_OF(x);
                    ROTATE_LEFT(s, x);
                }
                SET_COLOR(PARENT_OF(x), BLACK);
                SET_COLOR(PARENT_OF(PARENT_OF(x)), RED);
                if (PARENT_OF(PARENT_OF(x)) != NULL) {
                    ROTATE_RIGHT(s, PARENT_OF(PARENT_OF(x)));
                }
            }
        } else {
            node_t* y = LEFT_OF(PARENT_OF(PARENT_OF(x)));
            if (COLOR_OF(y) == RED) {
                SET_COLOR(PARENT_OF(x), BLACK);
                SET_COLOR(y, BLACK);
                SET_COLOR(PARENT_OF(PARENT_OF(x)), RED);
                x = PARENT_OF(PARENT_OF(x));
            } else {
                if (x == LEFT_OF(PARENT_OF(x))) {
                    x = PARENT_OF(x);
                    ROTATE_RIGHT(s, x);
                }
                SET_COLOR(PARENT_OF(x),  BLACK);
                SET_COLOR(PARENT_OF(PARENT_OF(x)), RED);
                if (PARENT_OF(PARENT_OF(x)) != NULL) {
                    ROTATE_LEFT(s, PARENT_OF(PARENT_OF(x)));
                }
            }
        }
    }
    node_t* ro = LDNODE(s, root);
    if (LDF(ro, c) != BLACK) {
        STF(ro, c, BLACK);
    }
}
Пример #2
0
DWORD DScm::CalcVerifyHash(CONST VCODE *vcode, VCPTR vdata, UINT vdata_size)
{
	DAssert(vcode && vdata && vdata_size);

	UINT op_num = DCount(vcode->op);
	DWORD hash = 0UL;
	BUFCPTR ptr = static_cast<BUFCPTR>(vdata);

	vdata_size >>= 2;

	for (UINT i = 0; i < vdata_size; i++, ptr += 4) {
		for (UINT j = 0; j < op_num; j++) {
			switch (vcode->op[j]) {
			case 0x00:
				hash ^= *reinterpret_cast<CONST DWORD *>(ptr);
				break;
			case 0x01:
				hash += *reinterpret_cast<CONST DWORD *>(ptr);
				break;
			case 0x02:
				hash -= *reinterpret_cast<CONST DWORD *>(ptr);
				break;
			case 0x03:
			case 0x04:
			case 0x05:
				hash ^= vcode->code[ptr[0]];
				hash ^= vcode->code[ptr[1]];
				hash ^= vcode->code[ptr[2]];
				hash ^= vcode->code[ptr[3]];
				break;
			case 0x06:
				hash = ROTATE_LEFT(hash, ptr[0]);
				hash = ROTATE_LEFT(hash, ptr[1]);
				hash = ROTATE_LEFT(hash, ptr[2]);
				hash = ROTATE_LEFT(hash, ptr[3]);
				break;
			case 0x07:
				hash = ROTATE_RIGHT(hash, ptr[0]);
				hash = ROTATE_RIGHT(hash, ptr[1]);
				hash = ROTATE_RIGHT(hash, ptr[2]);
				hash = ROTATE_RIGHT(hash, ptr[3]);
				break;
			default:
				break;
			}
		}
	}

	return hash;
}
Пример #3
0
void Cube::Ui()
{
    SaveState();
    subCubes[0][2][0] = ROTATE_RIGHT(oldSubCubes[2][2][0]);
    subCubes[1][2][0] = ROTATE_RIGHT(oldSubCubes[2][2][1]);
    subCubes[2][2][0] = ROTATE_RIGHT(oldSubCubes[2][2][2]);
    subCubes[0][2][1] = ROTATE_RIGHT(oldSubCubes[1][2][0]);
    subCubes[1][2][1] = ROTATE_RIGHT(oldSubCubes[1][2][1]);
    subCubes[2][2][1] = ROTATE_RIGHT(oldSubCubes[1][2][2]);
    subCubes[0][2][2] = ROTATE_RIGHT(oldSubCubes[0][2][0]);
    subCubes[1][2][2] = ROTATE_RIGHT(oldSubCubes[0][2][1]);
    subCubes[2][2][2] = ROTATE_RIGHT(oldSubCubes[0][2][2]);
}
Пример #4
0
void Cube::RotateRight()
{
    Ui();
    SaveState();
    subCubes[0][1][0] = ROTATE_RIGHT(oldSubCubes[2][1][0]);
    subCubes[1][1][0] = ROTATE_RIGHT(oldSubCubes[2][1][1]);
    subCubes[2][1][0] = ROTATE_RIGHT(oldSubCubes[2][1][2]);
    subCubes[0][1][1] = ROTATE_RIGHT(oldSubCubes[1][1][0]);
    subCubes[1][1][1] = ROTATE_RIGHT(oldSubCubes[1][1][1]);
    subCubes[2][1][1] = ROTATE_RIGHT(oldSubCubes[1][1][2]);
    subCubes[0][1][2] = ROTATE_RIGHT(oldSubCubes[0][1][0]);
    subCubes[1][1][2] = ROTATE_RIGHT(oldSubCubes[0][1][1]);
    subCubes[2][1][2] = ROTATE_RIGHT(oldSubCubes[0][1][2]);
    D();
}
Пример #5
0
int main()
{
  //unsigned int a;
  //scanf("%x", &a);
  //printf("%d\n", sizeof(a));
  //printf("%x\n", a);
  unsigned int a = 23;
  //printf("%c\n", a);  
  //printf("%08x\n", a);  
  //printf("%08x\n", ROTATE_LEFT (a, 8*sizeof(int), 0));
  //printf("%08x\n", ROTATE_LEFT (a, 8*sizeof(int), 0));
  //printf("%08x\n", ROTATE_LEFT (a, 8*sizeof(int), 0));
  printf("%08x\n", ROTATE_LEFT (a, 8*sizeof(int), 4));
  printf("%08x\n", ROTATE_RIGHT(a, 8*sizeof(int), 4));
  printf("%08x\n", ROTATE_RIGHT(a, 8*sizeof(int), 4));
  printf("%08x\n", ROTATE_RIGHT(a, 8*sizeof(int), 4));
  printf("%08x\n", ROTATE_RIGHT(a, 8*sizeof(int), 4));
  
  return 0;
}
Пример #6
0
/*Shift one row right*/
static int state_shift_row_right(byte_t * state,int n)
{
	if(n>3 || n<0 || !state) 
		return -1;
	if(n==0) return 0;
	word_t * swt = (word_t*)state;
	word_t sw=*swt;
	if(is_big_endian()){
		*swt=ROTATE_RIGHT(sw,32,n*8); 
	}else{
		*swt=ROTATE_LEFT(sw,32,n*8);
	}		

	return 0;
}
Пример #7
0
void key_expansion(const bw4 * key,word_t * w)
{
    bw  temp;
    int i,j;
    for(i=0;i<4;i++)
    {   
        w[i]=key->w[i];
    }   
    for(i=4;i<44;i++)
    {   
        temp.w=w[i-1];
        if(i%4==0)
        {   
            temp.w=ROTATE_RIGHT(temp.w,32,8);//this for little Endian (bigEndian:rotate right 24bit)
            subword(temp,sbox);
            temp.w^=RC[i/4-1];
        }   
        w[i]=w[i-4]^temp.w;
    }   
}
Пример #8
0
/*
 * Calculates/computes/determines a checksum for a file
 */
int
clip_FILECHECK(ClipMachine * ClipMachineMemory)
{
   char *uname = NULL;

   FILE *f = NULL;

   int c = 0, checksum = 0, fd = -1;

   if (ClipMachineMemory->argc < 1)
   {
      _clip_trap_err(ClipMachineMemory, EG_ARG, 0, 0, __FILE__, __LINE__, inv_arg);
		/*_clip_trap_printf( ClipMachineMemory, __FILE__, __LINE__, "invalid argument" );*/
      return 1;
   }
   if ((uname = _get_unix_name(ClipMachineMemory, _clip_parc(ClipMachineMemory, 1))) == NULL || (fd = open(uname, O_RDONLY)) == -1 || !_set_lock(fd, F_RDLCK) || (f = fdopen(fd, "r")) == NULL)
   {
      _clip_retni(ClipMachineMemory, -1);
      goto end;
   }

#define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
   while ((c = getc(f)) != EOF)
   {
      ROTATE_RIGHT(checksum);
      checksum += c;
      checksum &= 0xffff;
   }
#undef ROTATE_RIGHT

 end:
   _clip_retni(ClipMachineMemory, checksum);
   if (uname != NULL)
      free(uname);
   if (f != NULL)
      fclose(f);
   return 0;
}
Пример #9
0
/* =============================================================================
 * fixAfterDeletion
 * =============================================================================
 */
static void
fixAfterDeletion (rbtree_t* s, node_t* x)
{
    while (x != LDNODE(s,root) && COLOR_OF(x) == BLACK) {
        if (x == LEFT_OF(PARENT_OF(x))) {
            node_t* sib = RIGHT_OF(PARENT_OF(x));
            if (COLOR_OF(sib) == RED) {
                SET_COLOR(sib, BLACK);
                SET_COLOR(PARENT_OF(x), RED);
                ROTATE_LEFT(s, PARENT_OF(x));
                sib = RIGHT_OF(PARENT_OF(x));
            }
            if (COLOR_OF(LEFT_OF(sib)) == BLACK &&
                COLOR_OF(RIGHT_OF(sib)) == BLACK) {
                SET_COLOR(sib, RED);
                x = PARENT_OF(x);
            } else {
                if (COLOR_OF(RIGHT_OF(sib)) == BLACK) {
                    SET_COLOR(LEFT_OF(sib), BLACK);
                    SET_COLOR(sib, RED);
                    ROTATE_RIGHT(s, sib);
                    sib = RIGHT_OF(PARENT_OF(x));
                }
                SET_COLOR(sib, COLOR_OF(PARENT_OF(x)));
                SET_COLOR(PARENT_OF(x), BLACK);
                SET_COLOR(RIGHT_OF(sib), BLACK);
                ROTATE_LEFT(s, PARENT_OF(x));
                /* TODO: consider break ... */
                x = LDNODE(s,root);
            }
        } else { /* symmetric */
            node_t* sib = LEFT_OF(PARENT_OF(x));
            if (COLOR_OF(sib) == RED) {
                SET_COLOR(sib, BLACK);
                SET_COLOR(PARENT_OF(x), RED);
                ROTATE_RIGHT(s, PARENT_OF(x));
                sib = LEFT_OF(PARENT_OF(x));
            }
            if (COLOR_OF(RIGHT_OF(sib)) == BLACK &&
                COLOR_OF(LEFT_OF(sib)) == BLACK) {
                SET_COLOR(sib,  RED);
                x = PARENT_OF(x);
            } else {
                if (COLOR_OF(LEFT_OF(sib)) == BLACK) {
                    SET_COLOR(RIGHT_OF(sib), BLACK);
                    SET_COLOR(sib, RED);
                    ROTATE_LEFT(s, sib);
                    sib = LEFT_OF(PARENT_OF(x));
                }
                SET_COLOR(sib, COLOR_OF(PARENT_OF(x)));
                SET_COLOR(PARENT_OF(x), BLACK);
                SET_COLOR(LEFT_OF(sib), BLACK);
                ROTATE_RIGHT(s, PARENT_OF(x));
                /* TODO: consider break ... */
                x = LDNODE(s, root);
            }
        }
    }

    if (x != NULL && LDF(x,c) != BLACK) {
       STF(x, c, BLACK);
    }
}