Ejemplo n.º 1
0
UINT32 carray_search_back(const CARRAY *carray, const void *data, EC_BOOL (*cmp)(const void *, const void *))
{
    UINT32 pos;
    CARRAY_DATA_CMP data_cmp;

    if(NULL_PTR == cmp)
    {
        data_cmp = carray_data_cmp_default;
    }
    else
    {
        data_cmp = cmp;
    }

    CARRAY_LOCK(carray, LOC_CARRAY_0024);
    for(pos = carray->size; pos -- > 0; )
    {
        if(EC_TRUE == data_cmp(carray->data[ pos ], data))
        {
            CARRAY_UNLOCK(carray, LOC_CARRAY_0025);
            return pos;
        }
    }
    CARRAY_UNLOCK(carray, LOC_CARRAY_0026);
    return CARRAY_ERR_POS;
}
Ejemplo n.º 2
0
int compare_handled (cData *d1, cData *d2)
{
    HandledFrob *h1 = HANDLED_FROB(d1),
                *h2 = HANDLED_FROB(d2);

    if (h1->cclass != h2->cclass ||
	h1->handler != h2->handler)
	return 1;

    return data_cmp(&h1->rep, &h2->rep);
}
Ejemplo n.º 3
0
Int hash_find(Hash * hash, cData *key) {
    Int ind, i;

    ind = data_hash(key) % hash->hashtab_size;
    for (i = hash->hashtab[ind]; i != -1; i = hash->links[i]) {
	if (data_cmp(&hash->keys->el[i], key) == 0)
	    return i;
    }

    return F_FAILURE;
}
Ejemplo n.º 4
0
static Int search(cDict *dict, cData *key) {
    Int ind, i;

    ind = data_hash(key) % dict->hashtab_size;
    for (i = dict->hashtab[ind]; i != -1; i = dict->links[i]) {
        if (data_cmp(&dict->keys->el[i], key) == 0)
            return i;
    }

    return F_FAILURE;
}
Ejemplo n.º 5
0
Archivo: list.c Proyecto: nrhtr/genesis
Int list_search(cList * list, cData * data)
{
    cData *d, *start, *end;

    start = list->el + list->start;
    end = start + list->len;
    for (d = start; d < end; d++) {
        if (data_cmp(data, d) == 0)
            return d - start;
    }
    return -1;
}
Ejemplo n.º 6
0
Archivo: list.c Proyecto: nrhtr/genesis
int list_index(cList * list, cData * search, int origin)
{
    int len;
    Bool reverse = NO;
    cData *d, *start, *end;

    len = list_length(list);

    if (origin < 0) {
        reverse = YES;
        origin = -origin;
    }

    if (origin > len || !origin)
        return F_FAILURE;

    if (origin > len)
        return 0;

    origin--;
    start = list->el + list->start;
    end = start + list->len;

    if (reverse) {
        end -= (origin + 1);
        for (d = end; d >= start; d--) {
            if (data_cmp(search, d) == 0)
                return (d - start) + 1;
        }
    } else {
        for (d = start + origin; d < end; d++) {
            if (data_cmp(search, d) == 0)
                return (d - start) + 1;
        }
    }
    return 0;
}
Ejemplo n.º 7
0
/* Error-checking is the caller's responsibility; this routine assumes that it
 * will find the key in the dictionary. */
cDict *dict_del(cDict *dict, cData *key)
{
    Int ind, *ip, i = -1, j;

    dict = dict_prep(dict);

    /* Search for a pointer to the key, either in the hash table entry or in
     * the chain links. */
    ind = data_hash(key) % dict->hashtab_size;
    for (ip = &dict->hashtab[ind];; ip = &dict->links[*ip]) {
        i = *ip;
        if (data_cmp(&dict->keys->el[i], key) == 0)
            break;
    }

    /* Delete the element from the keys and values lists. */
    dict->keys = list_delete(dict->keys, i);
    dict->values = list_delete(dict->values, i);

    /* Replace the pointer to the key index with the next link. */
    *ip = dict->links[i];

    /* Copy the links beyond i backward. */
    MEMMOVE(dict->links + i, dict->links + i + 1, dict->keys->len - i);
    dict->links[dict->keys->len] = -1;

    /* Since we've renumbered all the elements beyond i, we have to check
     * all the links and hash table entries.  If they're greater than i,
     * decrement them.  Skip this step if the element we removed was the last
     * one. */
    if (i < dict->keys->len) {
        for (j = 0; j < dict->keys->len; j++) {
            if (dict->links[j] > i)
                dict->links[j]--;
        }
        for (j = 0; j < dict->hashtab_size; j++) {
            if (dict->hashtab[j] > i)
                dict->hashtab[j]--;
        }
    }

    return dict;
}
Ejemplo n.º 8
0
Archivo: list.c Proyecto: nrhtr/genesis
/* Effects: Returns 0 if the lists l1 and l2 are equivalent, or 1 if not. */
Int list_cmp(cList * l1, cList * l2)
{
    Int i, k;

    /* They're obviously the same if they're the same list. */
    if (l1 == l2)
        return 0;

    /* Lists can only be equal if they're of the same length. */
    if (l1->len != l2->len)
        return 1;

    /* See if any elements differ. */
    for (i = 0; i < l1->len; i++) {
        if ((k = data_cmp(&l1->el[l1->start + i], &l2->el[l2->start + i])) != 0)
            return k;
    }

    /* No elements differ, so the lists are the same. */
    return 0;
}
Ejemplo n.º 9
0
data_t * _any_cmp(data_t *self, char *name, arguments_t *args) {
  data_t *other = arguments_get_arg(args, 0);
  int_t  *ret;
  int     cmp = data_cmp(self, other);

  if (!strcmp(name, "==")) {
    ret = bool_get(cmp == 0);
  } else if (!strcmp(name, "!=")) {
    ret = bool_get(cmp != 0);
  } else if (!strcmp(name, ">")) {
    ret = bool_get(cmp > 0);
  } else if (!strcmp(name, ">=")) {
    ret = bool_get(cmp >= 0);
  } else if (!strcmp(name, "<")) {
    ret = bool_get(cmp < 0);
  } else if (!strcmp(name, "<=")) {
    ret = bool_get(cmp <= 0);
  } else {
    assert(0);
    ret = (int_t *) data_false();
  }
  return (data_t *) ret;
}
Ejemplo n.º 10
0
UINT32 carray_search_front_no_lock(const CARRAY *carray, const void *data, EC_BOOL (*cmp)(const void *, const void *))
{
    UINT32 pos;
    CARRAY_DATA_CMP data_cmp;

    if(NULL_PTR == cmp)
    {
        data_cmp = carray_data_cmp_default;
    }
    else
    {
        data_cmp = cmp;
    }

    for(pos = 0; pos < carray->size; pos ++)
    {
        if(EC_TRUE == data_cmp(carray->data[ pos ], data))
        {
            return pos;
        }
    }
    return CARRAY_ERR_POS;
}
Ejemplo n.º 11
0
Archivo: math.c Proyecto: nrhtr/genesis
/* which is 1 for max, -1 for min. */
INTERNAL void find_extreme(Int which)
{
    Int arg_start, num_args, i, type;
    cData *args, *extreme, d;

    arg_start = arg_starts[--arg_pos];
    args = &stack[arg_start];
    num_args = stack_pos - arg_start;

    if (!num_args) {
        cthrow(numargs_id, "Called with no arguments, requires at least one.");
        return;
    }

    type = args[0].type;
    if (type != INTEGER && type != STRING && type != FLOAT) {
        cthrow(type_id, "First argument (%D) not an integer, float or string.", &args[0]);
        return;
    }

    extreme = &args[0];
    for (i = 1; i < num_args; i++) {
        if (args[i].type != type) {
            cthrow(type_id, "Arguments are not all of same type.");
            return;
        }
        if (data_cmp(&args[i], extreme) * which > 0)
            extreme = &args[i];
    }

    /* Replace args[0] with extreme, and pop other arguments. */
    data_dup(&d, extreme);
    data_discard(&args[0]);
    args[0] = d;
    pop(num_args - 1);
}
Ejemplo n.º 12
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32L0xx HAL library initialization:
       - Configure the Flash prefetch, Flash preread and Buffer caches
       - Systick timer is configured by default as source of time base, but user 
             can eventually implement his proper time base source (a general purpose 
             timer for example or other time source), keeping in mind that Time base 
             duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
             handled in milliseconds basis.
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure the system clock to 2 MHz */
  SystemClock_Config();

#if defined(TERMINAL_IO_OUT)
  /* Configure Key Button */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
#else 
  /* Configure the COM port */
  UartHandle.Init.BaudRate = 115200;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits = UART_STOPBITS_1;
  UartHandle.Init.Parity = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode = UART_MODE_TX_RX;
  BSP_COM_Init(COM1, &UartHandle);
#endif

  /* Configures LED */
  BSP_LED_Init(LED2);

  /*##- Configure the CRYP peripheral ######################################*/
  /* Set the common CRYP parameters */
  CrypHandle.Instance = AES;
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Infinite loop */
  while (1)
  {

  /* Display Plain Data*/
  Display_PlainData(AES_TEXT_SIZE);
    
  /* Display Cypher Data*/
  Display_CypherData(AES_TEXT_SIZE);

#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif
 
  BSP_LED_Off(LED2);


    /******************************************************************************/
    /*                             AES mode ECB                                   */
    /******************************************************************************/

    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }

    /*=====================================================
        Encryption ECB mode
    ======================================================*/

    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey          = aAES128key;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESECB_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(ECB, 128, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler();
    }
    
    /* Compare the encrypted text with the expected one *************************/ 
    data_cmp(aEncryptedtext, aEncryptedtextECB, AES_TEXT_SIZE); 
	


#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif

    /*=====================================================
        Decryption ECB mode
    ======================================================*/
    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }
    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey    = aAES128key;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESECB_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(ECB, 128, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler();
    }
    /* Check the encrypted text with the expected one *************************/ 
    data_cmp(aDecryptedtext, aDecryptedtextECB, AES_TEXT_SIZE); 



#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif

    /******************************************************************************/
    /*                             AES mode CBC                                   */
    /******************************************************************************/

    /*=====================================================
        Encryption CBC mode
    ======================================================*/
    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }
    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESCBC_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(CBC, 128, AES_TEXT_SIZE);
    }
    else  
    {
      /* Processing Error */
      Error_Handler();
    }
    /* Check the encrypted text with the expected one *************************/ 
    data_cmp(aEncryptedtext, aCyphertext, AES_TEXT_SIZE); 



#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif

    /*=====================================================
        Decryption CBC mode
    ======================================================*/
    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }
    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESCBC_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(CBC, 128, AES_TEXT_SIZE);
    }
    else   
    {
      /* Processing Error */
      Error_Handler();
    }
    /* Check the encrypted text with the expected one *************************/ 
    data_cmp(aDecryptedtext, aDecryptedtextCBC, AES_TEXT_SIZE); 



#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif

    /******************************************************************************/
    /*                             AES mode CTR                                   */
    /******************************************************************************/

    /*=====================================================
        Encryption CTR mode
    ======================================================*/
    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }
    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESCTR_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(CTR, 128, AES_TEXT_SIZE);
    }
    else     
    {
      /* Processing Error */
      Error_Handler();
    }
    
    /* Check the encrypted text with the expected one *************************/ 
    data_cmp(aEncryptedtext, aEncryptedtextCTR, AES_TEXT_SIZE); 



#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif
    /*=====================================================
        Decryption CTR mode
    ======================================================*/
    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }
    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESCTR_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(CTR, 128, AES_TEXT_SIZE);
    }
    else  
    {
      /* Processing Error */
      Error_Handler();

	  }

    /* Check the encrypted text with the expected one *************************/ 
    data_cmp(aDecryptedtext, aDecryptedtextCTR, AES_TEXT_SIZE); 
	



#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif

  BSP_LED_On(LED2);


    printf("\n\r Example restarted...\n ");


  }
}