lhash在C - Linux中的lh_insert崩溃了吗?

时间:2015-11-13 02:00:32

标签: c linux openssl

我正在尝试编译示例程序以使用lhash。我看不到关于lhash的好教程。所以,我理解lhash的唯一方法是使用lhash linux手册页。这是一个例子,我想做。但是,在执行lh_insert时我遇到了崩溃。我对它为什么会发生无能为力。

/** In order to compile this program do the following **/

/**  gcc lhastEx.c -lcrypto -o lhastEx.out **/

/** Install the openssl dev library on ubuntu by -- sudo apt-get install libssl-dev **/
/*** This is needed for library hash -- basically open ssl ones **/
#include <openssl/lhash.h>

/** Hash table -- just like maps in C++ i.e. QMAP -- it needs a key and the value **/

/*I have got a prints to check the flow */
#define __DBG (1)

static void dbgMsg(const char *msg)
{
#if __DBG
   printf("%s",msg);
#endif 
}


static int cmpFunc(const void *src, const void *dest)
{
    dbgMsg("cmpFunc called..\r\n");

    const int *obj1 = src;
    const int *obj2 = dest;

    return memcmp(obj1, obj2, sizeof(int));

}

static unsigned long keyHash(const void *entry)
{
    unsigned long int hash = 0;
    const int *val = entry;
    dbgMsg("keyHash method invoked\r\n");

    hash |= *(val);

    return hash;
}

int main(int argc, char *argv[])
{
   int *hashKey2 = malloc(sizeof(int));

   int *hashKey3 = malloc(sizeof(int));

   int *hashKey1 = malloc(sizeof(int));

   *hashKey1 = 10;
   *hashKey2 = 20;
   *hashKey3 = 30;

   /* we can make a function to generate this key unique **/
   /** Ideally, this 1 should be a unique hash value **/

    /************** Created the hash table now -- I see this as equivalent to the map in C ++ or QtMap **/   
    LHASH_OF(int) *hashtable = lh_new(keyHash, cmpFunc);
   /*** add a new entry now **/


   lh_insert(hashtable, hashKey2);


  return 0;

}

2 个答案:

答案 0 :(得分:1)

LHASH API可以(有点)使用STACK API。此外,还有OpenSSL源代码可以帮助您了解它。例如,请参阅使用crypto/err/err.c哈希表的ERR_STRING_DATA,并查看lhash.hsafestack.h的宏定义。

最安全的方法是为表定义强类型函数。在这种情况下,一个关键方面是哈希表中的元素必须是结构类型。通过使用带有单个int字段的结构,请参阅下面的代码,了解它如何对整数哈希表起作用。

#include <openssl/lhash.h>
#include <string.h>

#define __DBG (1)

static void dbgMsg(const char *msg)
{
#if __DBG
    printf("%s", msg);
#endif 
}

typedef struct int_value_st {
    int value;
} INT_VALUE;

static int int_value_cmp(const INT_VALUE *a, const INT_VALUE *b)
{
    dbgMsg("cmpFunc called..\r\n");
    return a->value - b->value;
}
static IMPLEMENT_LHASH_COMP_FN(int_value, INT_VALUE);

static unsigned long int_value_hash(const INT_VALUE *entry)
{
    unsigned long int hash = 0;
    dbgMsg("keyHash method invoked\r\n");

    hash |= entry->value;

    return hash;
}
static IMPLEMENT_LHASH_HASH_FN(int_value, INT_VALUE);

/* See stack/safestack.h for a complete list of the possible #defines */
#define lh_INT_VALUE_new() LHM_lh_new(INT_VALUE,int_value)
#define lh_INT_VALUE_insert(lh,inst) LHM_lh_insert(INT_VALUE,lh,inst)
#define lh_INT_VALUE_retrieve(lh,inst) LHM_lh_retrieve(INT_VALUE,lh,inst)
#define lh_INT_VALUE_delete(lh,inst) LHM_lh_delete(INT_VALUE,lh,inst)
#define lh_INT_VALUE_free(lh) LHM_lh_free(INT_VALUE,lh)

int LHashTest(void)
{
    DECLARE_LHASH_OF(INT_VALUE);

    INT_VALUE *hashKey1 = OPENSSL_malloc(sizeof(*hashKey1));
    INT_VALUE *hashKey2 = OPENSSL_malloc(sizeof(*hashKey2));
    INT_VALUE *hashKey3 = OPENSSL_malloc(sizeof(*hashKey3));
    INT_VALUE *hashKeyFound = NULL;

    hashKey1->value = 10;
    hashKey2->value = 20;
    hashKey3->value = 30;

    LHASH_OF(INT_VALUE) *hashtable = NULL;
    hashtable = lh_INT_VALUE_new();
    lh_INT_VALUE_insert(hashtable, hashKey1);
    lh_INT_VALUE_insert(hashtable, hashKey2);
    lh_INT_VALUE_insert(hashtable, hashKey3);

    /* Should find result */
    hashKeyFound = lh_INT_VALUE_retrieve(hashtable, hashKey2);
    lh_INT_VALUE_delete(hashtable, hashKeyFound);
    /* Should not find result */
    hashKeyFound = lh_INT_VALUE_retrieve(hashtable, hashKey2);

    /* OPENSSL_free()s all elements */
    lh_INT_VALUE_free(hashtable);

    return 1;
}

顺便说一句,看起来你忽略了一些编译器警告......这通常不是一件好事。

答案 1 :(得分:1)

我发现这是由于open-ssl的后向不兼容版本造成的。这是纠正 -

  _LHASH *hashtable = lh_new(keyHash, cmpFunc);

它运作良好。这是现在的整个代码。这可能有助于一些新人尝试lhash。虽然,我认为C ++提供了更清晰的散列方式而不是C.我不喜欢这个库。但是,它用于旧项目。

/** In order to compile this program do the following **/

/**  gcc lhastEx.c -lcrypto -o lhastEx.out **/

/** Install the openssl dev library on ubuntu by -- sudo apt-get install libssl-dev **/
/*** This is needed for library hash -- basically open ssl ones **/
#include <openssl/lhash.h>

/** Hash table -- just like maps in C++ i.e. QMAP -- it needs a key and the value **/

/*I have got a prints to check the flow */
#define __DBG (1)

static void dbgMsg(const char *msg)
{
#if __DBG
   printf("%s",msg);
#endif 
}


static int cmpFunc(const void *src, const void *dest)
{
    dbgMsg("cmpFunc called..\r\n");

    const int *obj1 = src;
    const int *obj2 = dest;

    return memcmp(obj1, obj2, sizeof(int));

}

static unsigned long keyHash(const void *entry)
{
    unsigned long int hash = 0;
    const int *val = entry;
    dbgMsg("keyHash method invoked\r\n");

    hash |= *(val);

    return hash;
}

int main(int argc, char *argv[])
{
   int inputBucket = 0;
   int *hashKey2 = malloc(sizeof(int));

   int *hashKey3 = malloc(sizeof(int));

   int *hashKey1 = malloc(sizeof(int));

   *hashKey1 = 10;
   *hashKey2 = 20;
   *hashKey3 = 30;

   int *ptrInputBucket = &inputBucket;

   /* we can make a function to generate this key unique **/
   /** Ideally, this 1 should be a unique hash value **/

    /************** Created the hash table now -- I see this as equivalent to the map in C ++ or QtMap **/   
    _LHASH *hashtable = lh_new(keyHash, cmpFunc);
   /*** add a new entry now **/

   lh_insert(hashtable, hashKey2);
   lh_insert(hashtable, hashKey3);

   /** now retrieve the data **/
   ptrInputBucket = lh_retrieve(hashtable, hashKey2);

   if( ptrInputBucket != NULL )
    {
       printf("The value retrieve from Hash Table is %d\r\n", *ptrInputBucket);
    }
  return 0;

}