与无效*指针的typedef功能 [英] Typedef function with void * pointer

查看:325
本文介绍了与无效*指针的typedef功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在一个库中定义的函数,但我有问题,使用它。 问题,帮我实现它,但我不能使用此功能。

下面是typedef的:

无效的typedef(* paillier_get_rand_t)(void *的BUF,INT LEN);

这里是我如何实现它:

 无效get_rand(void *的BUF,INT LEN){
  BUF =(无效*)兰特(); //我不知道,如果它的工作原理,但不是问题
}

在这里,我如何创建函数指针并调用它。

  INT模数= 4;
    无效* BUF;
    paillier_pubkey_t *酒吧;
    paillier_prvkey_t * PRV;
    paillier_keygen(模数,和放大器;酒吧,和放大器; PRV,&安培; get_rand);

问题1)如何发送参数BUF和len?
问题2)现在的错误是不同的:

 引用`paillier_keygen(INT,paillier_pubkey_t **,** paillier_prvkey_t,无效(*)(无效*,INT))'未定义

功能 paillier_keygen 用于生成的算法respectives键和功能 get_rand 用于获取由概率算法所需的随机性。

因为我需要做的是要能够用lib我实施这个的typedef。

编辑:

我发现谁是已经定义了一个功能,我可以使用一个叫做 paillier_get_rand_devurandom

/ *
    这些功能可以被传递给paillier_keygen和
    paillier_enc函数提供随机数的源。该
    首先读取从/ dev /随机字节。在Linux上,该装置
    独家返回熵环境噪声收集和
    因此,当没有足够的可用频繁块。第二
    返回从/ dev / urandom的字节。在Linux上,该设备还返回
    环境噪声,而是用一个伪随机数增强它
    生成器时没有足够的可用。后者可能是
    更好的选择,除非你有特别的理由相信它是
    不足。
* /

 无效paillier_get_rand_devurandom(void *的BUF,INT LEN);

如果我之前

用于像

  paillier_keygen(模数,和放大器;酒吧,和放大器; PRV,paillier_get_rand_devurandom(BUF,4));

我得到错误:无效使用作废前pression的

如果我用的是这样的:

  paillier_keygen(模数,和放大器;酒吧,和放大器; PRV,&安培; paillier_get_rand_devurandom);

我得到:

 未定义的参考`paillier_get_rand_devurandom(无效*,INT)
未定义的引用`paillier_keygen(INT,paillier_pubkey_t **,** paillier_prvkey_t,无效(*)(无效*,INT))

该库包括在内。在这里,我想补充有关签名的详细信息:

/ *
  使用随机从生成长度modulusbits的密钥对
  提供get_rand功能。空间将被分配给每个的
  键,以及给定的指针将被设置为指向新
  paillier_pubkey_t和paillier_prvkey_t结构。功能
  paillier_get_rand_devrandom和paillier_get_rand_devurandom可能
  作为最后一个参数传递。
* /

 无效paillier_keygen(INT modulusbits,
            paillier_pubkey_t **酒馆,paillier_prvkey_t ** PRV,paillier_get_rand_t get_rand);

/ *
  这是用于获取的回调函数的类型
  由概率算法所需的随意性。功能
  paillier_get_rand_devrandom和paillier_get_rand_devurandom
  (后来证明),可以传递给需要的库函数
  paillier_get_rand_t,或者你可以实现你自己的。如果您实现
  自己这样的功能,应填写LEN中的随机字节
  阵BUF。
* /

 无效的typedef(* paillier_get_rand_t)(void *的BUF,INT LEN);


解决方案

我找到了解决办法。首先我导入库类似的extern,因为 paillier.h 是用C写的和我使用C ++。

就像这样:

 使用的#include< gmp.h>
为externC{
    #包括paillier.h
}

在那之后我也编译了 -lpaillier -lgmp

最后code将是:

  paillier_pubkey_t *酒吧;
    paillier_prvkey_t * PRV;
    paillier_keygen(1024,和放大器;酒吧,和放大器; PRV,paillier_get_rand_devurandom);

有没有必要定义任何附加功能。

I'm trying to implement a function defined in a library but I'm having problems to use it. This question help me to implement it, but I couldn't use the function.

Here is the typedef:

typedef void (*paillier_get_rand_t) ( void* buf, int len );

And here is how I implement it:

void get_rand(void* buf, int len) {
  buf = (void *)rand();  // I don't know if it works but isn't the problem
}

And here how I create the pointer to the function and call it.

   int modulus = 4;
    void* buf;
    paillier_pubkey_t* pub;
    paillier_prvkey_t* prv;
    paillier_keygen(modulus, &pub, &prv, &get_rand);                

Question 1) How I send the parameters buf and len? Question 2) Now the error is different:

reference to `paillier_keygen(int, paillier_pubkey_t**, paillier_prvkey_t**, void (*)(void*, int))' undefined

The function paillier_keygen is used to generate the respectives key for the algorithm and the function get_rand is used to obtain the randomness needed by the probabilistic algorithms.

I'm implementing this typedef because I need to do it to be able to use the lib.

EDIT:

I found one function who is already defined and I can use called paillier_get_rand_devurandom.

/* These functions may be passed to the paillier_keygen and paillier_enc functions to provide a source of random numbers. The first reads bytes from /dev/random. On Linux, this device exclusively returns entropy gathered from environmental noise and therefore frequently blocks when not enough is available. The second returns bytes from /dev/urandom. On Linux, this device also returns environmental noise, but augments it with a pseudo-random number generator when not enough is available. The latter is probably the better choice unless you have a specific reason to believe it is insufficient. */

void paillier_get_rand_devurandom( void* buf, int len );

If I used like before

paillier_keygen(modulus, &pub, &prv, paillier_get_rand_devurandom(buf, 4));

I get error: invalid use of void expression

If I used like this:

paillier_keygen(modulus, &pub, &prv, &paillier_get_rand_devurandom);

I get:

undefined reference to `paillier_get_rand_devurandom(void*, int)'
undefined reference to `paillier_keygen(int, paillier_pubkey_t**, paillier_prvkey_t**, void (*)(void*, int))'

The library is included. Here I add more info about the signatures:

/* Generate a keypair of length modulusbits using randomness from the provided get_rand function. Space will be allocated for each of the keys, and the given pointers will be set to point to the new paillier_pubkey_t and paillier_prvkey_t structures. The functions paillier_get_rand_devrandom and paillier_get_rand_devurandom may be passed as the final argument. */

void paillier_keygen( int modulusbits,
            paillier_pubkey_t** pub, paillier_prvkey_t** prv, paillier_get_rand_t get_rand );

/* This is the type of the callback functions used to obtain the randomness needed by the probabilistic algorithms. The functions paillier_get_rand_devrandom and paillier_get_rand_devurandom (documented later) may be passed to any library function requiring a paillier_get_rand_t, or you may implement your own. If you implement your own such function, it should fill in "len" random bytes in the array "buf". */

typedef void (*paillier_get_rand_t) ( void* buf, int len );

解决方案

I found the solution. First of all I import the library like extern, because paillier.h is written in C and I'm using C++.

Just like this:

use #include <gmp.h>
extern "C"{
    #include "paillier.h"
}

After that I also compiled with -lpaillier and also -lgmp.

The final code would be:

paillier_pubkey_t* pub;
    paillier_prvkey_t* prv;
    paillier_keygen(1024, &pub, &prv, paillier_get_rand_devurandom);

There is no need to define any additional function.

这篇关于与无效*指针的typedef功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆