예제 #1
0
파일: test.cpp 프로젝트: Ingwar/amuse
int main(int argc, const char* argv[])
{
    struct simple_hash ht;
    init_hash(&ht,128);
    for (;;)
    {
        char buf[256];
        if (fgets(buf, 256, stdin) == NULL)
            break;
        char *command = strtok(buf, whitespace);
        unsigned int key;
        unsigned int value;
        if (strcmp(command, "insert") == 0)
        {
            sscanf(strtok(NULL, whitespace), "%u", &key);
            sscanf(strtok(NULL, whitespace), "%u", &value);
            hash_insert(&ht,key,value);
        }
        else if (strcmp(command, "lookup") == 0)
        {
            sscanf(strtok(NULL, whitespace), "%u", &key);
            size_t value;
            int result;
            result=hash_lookup(&ht,key, &value);
            if (result==0)
                printf("%u\n", (unsigned int) value);
            else
                printf("None\n");
        }
        else if (strcmp(command, "increment") == 0)
        {
            sscanf(strtok(NULL, whitespace), "%u", &key);
            hash_cell_insert(&ht,key)->value++;
        }
        else if (strcmp(command, "delete") == 0)
        {
            sscanf(strtok(NULL, whitespace), "%u", &key);
            hash_delete(&ht,key);
        }
        else if (strcmp(command, "clear") == 0)
        {
            clear_hash(&ht);
        }
        else if (strcmp(command, "compact") == 0)
        {
            compact_hash(&ht);
        }
        fflush(stdout);
    }

    // Dump entire table
    printf("{\n");
    if(ht.m_zeroUsed) printf("    %u: %u,\n", ht.m_zeroCell.key, ht.m_zeroCell.value);
    for(size_t i=0;i<ht.m_arraySize;i++) if(ht.m_cells[i].key) printf("    %u: %u,\n", ht.m_cells[i].key, ht.m_cells[i].value);
    printf("}\n");
    end_hash(&ht);
    return 0;
}
예제 #2
0
clear_block (gif_dest_ptr dinfo)
/* Reset compressor and issue a Clear code */
{
  clear_hash(dinfo);			/* delete all the symbols */
  dinfo->free_code = dinfo->ClearCode + 2;
  output(dinfo, dinfo->ClearCode);	/* inform decoder */
  dinfo->n_bits = dinfo->init_bits;	/* reset code size */
  dinfo->maxcode = MAXCODE(dinfo->n_bits);
}
예제 #3
0
파일: indexed.hpp 프로젝트: zjucsxxd/carmel
 /// this never shrinks our previously allocated capacity
 void init_empty_hash(I capacityPowerOf2) {
   if (capacityPowerOf2 < 4) capacityPowerOf2 = 4;
   assert(is_power_of_2(capacityPowerOf2));
   I const newmask = capacityPowerOf2 - 1;
   if (!index_ || mask_ < newmask) {
     freehash();
     mask_ = newmask;
     index_ = (Indices)std::malloc(sizeof(I) * capacityPowerOf2);
     setGrowAt(capacityPowerOf2);
   }
   clear_hash();
 }
예제 #4
0
compress_init (gif_dest_ptr dinfo, int i_bits)
/* Initialize LZW compressor */
{
  /* init all the state variables */
  dinfo->n_bits = dinfo->init_bits = i_bits;
  dinfo->maxcode = MAXCODE(dinfo->n_bits);
  dinfo->ClearCode = ((code_int) 1 << (i_bits - 1));
  dinfo->EOFCode = dinfo->ClearCode + 1;
  dinfo->free_code = dinfo->ClearCode + 2;
  dinfo->first_byte = TRUE;	/* no waiting symbol yet */
  /* init output buffering vars */
  dinfo->bytesinpkt = 0;
  dinfo->cur_accum = 0;
  dinfo->cur_bits = 0;
  /* clear hash table */
  clear_hash(dinfo);
  /* GIF specifies an initial Clear code */
  output(dinfo, dinfo->ClearCode);
}
예제 #5
0
void
handle_packet(const u_char *packet, int length) {
  static int n = 0;
  static unsigned char initial_hello[] = { 
    0x16, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 
  };
  uint8 *data; 
  size_t data_length, rlen;
  int i, res;
#if DTLS_VERSION == 0xfeff
#ifndef SHA1_DIGEST_LENGTH
#define SHA1_DIGEST_LENGTH 20
#endif
  uint8 hash_buf[16 + SHA1_DIGEST_LENGTH];
#elif DTLS_VERSION == 0xfefd
  uint8 hash_buf[DTLS_SHA256_DIGEST_LENGTH];
#endif
#define verify_data_length 12
  int is_client;
  n++;

  SKIP_ETH_HEADER(packet, length);
  SKIP_IP_HEADER(packet, length);

  /* determine from port if this is a client */
  is_client = dtls_uint16_to_int(packet) != 20220;

  SKIP_UDP_HEADER(packet, length);

  while (length) {
    rlen = dtls_uint16_to_int(packet + 11) + sizeof(dtls_record_header_t);

    if (!rlen) {
      fprintf(stderr, "invalid length!\n");
      return;
    }

    /* skip packet if it is from a different epoch */
    if (dtls_uint16_to_int(packet + 3) != epoch[is_client])
      goto next;

    res = decrypt_verify(is_client, packet, rlen,
			 &data, &data_length);

    if (res <= 0)
      goto next;
    
    printf("packet %d (from %s):\n", n, is_client ? "client" : "server");
    hexdump(packet, sizeof(dtls_record_header_t));
    printf("\n");
    hexdump(data, data_length);
    printf("\n");
    
    if (packet[0] == 22 && data[0] == 1) { /* ClientHello */
      if (memcmp(packet, initial_hello, sizeof(initial_hello)) == 0)
	goto next;
	
      memcpy(dtls_kb_client_iv(OTHER_CONFIG), data + 14, 32);

	clear_hash();
#if DTLS_VERSION == 0xfeff
      hs_hash[0] = dtls_new_hash(HASH_MD5);
      hs_hash[1] = dtls_new_hash(HASH_SHA1);

      hs_hash[0]->init(hs_hash[0]->data);
      hs_hash[1]->init(hs_hash[1]->data);
#elif DTLS_VERSION == 0xfefd
      dtls_hash_init(hs_hash[0]);
#endif
    }
    
    if (packet[0] == 22 && data[0] == 2) { /* ServerHello */
      memcpy(dtls_kb_server_iv(OTHER_CONFIG), data + 14, 32);
      /* FIXME: search in ciphers */
      OTHER_CONFIG->cipher = TLS_PSK_WITH_AES_128_CCM_8;
    }
    
    if (packet[0] == 20 && data[0] == 1) { /* ChangeCipherSpec */
      printf("client random: ");
      dump(dtls_kb_client_iv(OTHER_CONFIG), 32);
      printf("\nserver random: ");
      dump(dtls_kb_server_iv(OTHER_CONFIG), 32);
      printf("\n");
      master_secret_len = 
	dtls_prf(pre_master_secret, pre_master_len,
		 (unsigned char *)"master secret", 13,
		 dtls_kb_client_iv(OTHER_CONFIG), 32,
		 dtls_kb_server_iv(OTHER_CONFIG), 32,
		 master_secret, DTLS_MASTER_SECRET_LENGTH);
  
      printf("master_secret:\n  ");
      for(i = 0; i < master_secret_len; i++) 
	printf("%02x", master_secret[i]);
      printf("\n");

      /* create key_block from master_secret
       * key_block = PRF(master_secret,
                     "key expansion" + server_random + client_random) */
      dtls_prf(master_secret, master_secret_len,
	       (unsigned char *)"key expansion", 13,
	       dtls_kb_server_iv(OTHER_CONFIG), 32,
	       dtls_kb_client_iv(OTHER_CONFIG), 32,
	       OTHER_CONFIG->key_block, 
	       dtls_kb_size(OTHER_CONFIG));

      OTHER_CONFIG->read_cipher = 
	dtls_cipher_new(OTHER_CONFIG->cipher,
			dtls_kb_client_write_key(OTHER_CONFIG),
			dtls_kb_key_size(OTHER_CONFIG));

      if (!OTHER_CONFIG->read_cipher) {
	warn("cannot create read cipher\n");
      } else {
	dtls_cipher_set_iv(OTHER_CONFIG->read_cipher,
			   dtls_kb_client_iv(OTHER_CONFIG),
			   dtls_kb_iv_size(OTHER_CONFIG));
      }

      OTHER_CONFIG->write_cipher = 
	dtls_cipher_new(OTHER_CONFIG->cipher, 
			dtls_kb_server_write_key(OTHER_CONFIG),
			dtls_kb_key_size(OTHER_CONFIG));
      
      if (!OTHER_CONFIG->write_cipher) {
	warn("cannot create write cipher\n");
      } else {
	dtls_cipher_set_iv(OTHER_CONFIG->write_cipher,
			   dtls_kb_server_iv(OTHER_CONFIG),
			   dtls_kb_iv_size(OTHER_CONFIG));
      }

      /* if (is_client) */
	SWITCH_CONFIG;
      epoch[is_client]++;

      printf("key_block:\n");
      printf("  client_MAC_secret:\t");  
      dump(dtls_kb_client_mac_secret(CURRENT_CONFIG), 
	   dtls_kb_mac_secret_size(CURRENT_CONFIG));
      printf("\n");

      printf("  server_MAC_secret:\t");  
      dump(dtls_kb_server_mac_secret(CURRENT_CONFIG), 
	   dtls_kb_mac_secret_size(CURRENT_CONFIG));
      printf("\n");

      printf("  client_write_key:\t");  
      dump(dtls_kb_client_write_key(CURRENT_CONFIG), 
	   dtls_kb_key_size(CURRENT_CONFIG));
      printf("\n");

      printf("  server_write_key:\t");  
      dump(dtls_kb_server_write_key(CURRENT_CONFIG), 
	   dtls_kb_key_size(CURRENT_CONFIG));
      printf("\n");

      printf("  client_IV:\t\t");  
      dump(dtls_kb_client_iv(CURRENT_CONFIG), 
	   dtls_kb_iv_size(CURRENT_CONFIG));
      printf("\n");
      
      printf("  server_IV:\t\t");  
      dump(dtls_kb_server_iv(CURRENT_CONFIG), 
	   dtls_kb_iv_size(CURRENT_CONFIG));
      printf("\n");
      
    }

    if (packet[0] == 22) {
      if (data[0] == 20) { /* Finished */
	finalize_hash(hash_buf);
	/* clear_hash(); */

	update_hash((unsigned char *)packet, sizeof(dtls_record_header_t),
		    data, data_length);

	dtls_prf(master_secret, master_secret_len,
		 is_client 
		 ? (unsigned char *)"client finished" 
		 : (unsigned char *)"server finished" 
		 , 15,
		 hash_buf, sizeof(hash_buf),
		 NULL, 0,
		 data + sizeof(dtls_handshake_header_t),
		 verify_data_length);
	printf("verify_data:\n");
	dump(data, data_length);
	printf("\n");
      } else {
	update_hash((unsigned char *)packet, sizeof(dtls_record_header_t),
		    data, data_length);
      }
    }

    if (packet[0] == 23) {	/* Application Data */
      printf("Application Data:\n");
      dump(data, data_length);
      printf("\n");
    }

  next:
    length -= rlen;
    packet += rlen;
  }
}
예제 #6
0
파일: indexed.hpp 프로젝트: zjucsxxd/carmel
 void clear() {
   clear_destroy(vals_);
   clear_hash();
 }
예제 #7
0
파일: fen.c 프로젝트: caissist/danasah-500
/*recibimos una posición en formato FEN del GUI cuando editamos y la configuramos para que la entienda el motor*/
void SetBoard(char *string)
{
	char c, r;
	int sq=0, i=0, j=0;
	/*tipo de pieza según la letra que recibimos, ejemplo B es bishop (nosotros la tenemos definida con el número 2*/
	int	tpieza[26] = {6,2,6,6,6,6,6,6,6,6,5,6,6,1,6,0,4,3,6,6,6,6,6,6,6,6};
	int ntb = 0, ntn = 0;

	/*número de piezas iniciales*/
	peonesblancos = peonesnegros = 0;
	caballosblancos = caballosnegros = 0;
	alfilesblancos = alfilesnegros = 0;
	torresblancas = torresnegras = 0;
	damasblancas = damasnegras = 0;

	/*Rellenamos la tabla de posiciones y colores*/
	while (sq < 64) {
		enroque_mascara[sq] = 15;
		c = string[i++];
		if ('a'<c && c<'z') {
			color[sq] = NEGRO;
			pieza[sq] = tpieza[c - 'a'];
			switch (pieza[sq]) {
				case PEON:
					peonesnegros++;
					break;
				case CABALLO:
					caballosnegros++;
					break;
				case ALFIL:
					alfilesnegros++;
					break;
				case TORRE:
					ntn++;
					torresnegras++;
					if (ntn == 1) tn2 = sq;
					else if (ntn == 2) tn1 = sq;
					break;
				case DAMA:
					damasnegras++;
					break;
				case REY:
					rn = sq;
					break;
			}
			sq++;
		}
		else if ('A'<c && c<'Z') {
			color[sq] = BLANCO;
			pieza[sq] = tpieza[c - 'A'];
			switch (pieza[sq]) {
				case PEON:
					peonesblancos++;
					break;
				case CABALLO:
					caballosblancos++;
					break;

				case ALFIL:
					alfilesblancos++;
					break;
				case TORRE:
					ntb++;
					torresblancas++;
					if (ntb == 1) tb2 = sq;
					else if (ntb == 2) tb1 = sq;
					break;
				case DAMA:
					damasblancas++;
					break;
				case REY:
					rb = sq;
					break;
			}
			sq++;
		}
		else if ('1'<=c && c<='8') {
			for (j=0; j<(c - '0'); j++) {
				color[sq+j] = VACIO;
				pieza[sq+j] = VACIO;
			}
			sq += j;
		}
	}

	/*Color para el turno*/
	c = string[i++];
	if (c == 'w')		{ turno = BLANCO; }
	else if (c == 'b')	{ turno = NEGRO; }

	motor = VACIO;

    /*Flag del enroque*/
	if (strcmp (variante,"fischerandom") == 0) {
		if (strcmp (castling,"HAha") == 0) {
			enroque = 0;
			c = string[i++];
			while ((c >= 'A' && c <= 'H') || (c >= 'a' && c <= 'h')) {
				if (c >= 'A' && c <= 'H') {
					r = 65 + COLUMNA(rb);
					if (c > r) {
						enroque = enroque + 1;
						tb1 = c - 'A' + 56;
					}
					if (c < r) {
						enroque = enroque + 2;
						tb2 = c - 'A' + 56;
					}
				}
				if (c >= 'a' && c <= 'h') {
					r = 97 + COLUMNA(rn);
					if (c > r) {
						enroque = enroque + 4;
						tn1 = c - 'a';
					}
					if (c < r) {
						enroque = enroque + 8;
						tn2 = c - 'a';
					}
				}
				c = string[i++];
			}
		}
		if (strcmp (castling,"OO") == 0) {
			enroque = 0;
			c = string[i++];
			while (c == 'K' || c == 'Q' || c == 'k' || c == 'q') {
				if (c == 'K') {
					enroque = enroque + 1;
				}
				if (c == 'Q') {
					enroque = enroque + 2;
				}
				if (c == 'k') {
					enroque = enroque + 4;
				}
				if (c == 'q') {
					enroque = enroque + 8;
				}
				c = string[i++];
			}
		}
	}
	else {
		enroque = 0;
		c = string[i++];
		while (c == 'K' || c == 'Q' || c == 'k' || c == 'q') {
			if (c == 'K') {
				enroque = enroque + 1;
				tb1 = 63;
			}
			if (c == 'Q') {
				enroque = enroque + 2;
				tb2 = 56;
			}
			if (c == 'k') {
				enroque = enroque + 4;
				tn1 = 7;
			}
			if (c == 'q') {
				enroque = enroque + 8;
				tn2 = 0;
			}
			c = string[i++];
		}
	}

	/*máscaras para los enroques*/
	enroque_mascara[rb] = 12;
	enroque_mascara[rn] = 3;
	enroque_mascara[tb2] = 13;
	enroque_mascara[tb1] = 14;
	enroque_mascara[tn2] = 7;
	enroque_mascara[tn1] = 11;

	/*Flag de al paso*/
	c = string[i++];
	if (c>='a' && c<='h') {
		alpaso = (c - 'a');
		c = string[i++];
		if (c>='1' && c<='8') alpaso += 8*(7-(c - '1'));
		else alpaso = -1;
	}
	else alpaso = -1;


	/*no comprobamos en el fen la regla de 50 movimientos o el número de jugadas, la ponemos directamente a 0*/
	regla50 = 0;	        /*inicamos la regla de los 50 movimientos*/
	njugadas = 0;	        /*ponemos a 0 el contador de jugadas*/

	fuera_del_libro = 0;
	hacer_ponder = FALSO;	/*inicialmente tenemos que buscar más que ponderar*/

	Calcula_material();		/*calculamos el material en centipeones*/

	/*calculamos las distancias entre casillas*/
	for (i=0;i<64;i++) {
		for (j=0;j<64;j++) {
			distancia[i][j] = MAX(abs(FILA(j) - FILA(i)), abs(COLUMNA(j) - COLUMNA(i)));
		}
	}

	clear_hash();			/*borramos las tablas hash*/
	poner_hash();			/*calculamos la llave hash para la posición*/
}