gpt4 book ai didi

c - BIO_get_mem_ptr 返回 0 作为长度和输出

转载 作者:太空宇宙 更新时间:2023-11-04 03:24:18 24 4
gpt4 key购买 nike

这里我试图获取 bio 文件的大小,

long res = BIO_get_mem_ptr(certBio, &bptr);
length = bptr->length; // is the length

我从一些 stackoverflow 问题中得到了这个示例代码。我已经尝试了很多次,但是 BIO_get_mem_ptrbptr 中给出了一个 null 指针,返回值是 0。我在任何引用站点中都找不到与此问题相关的任何解决方案。

这里是源码,

int pass_cb(char *buf, int size, int rwflag, void *u)
{
int len;
char *tmp;
tmp = "123456";
len = strlen(tmp);

if (len <= 0)
return 0;
if (len > size)
len = size;
memcpy(buf, tmp, len);
return len;
}

int main(void)
{
X509 *x509;
int length = 0;

unsigned char data[1000];
unsigned char *buffer = NULL;
buffer = data;

BIO *certBio = BIO_new(BIO_s_file());
// BIO *certBio = BIO_new(BIO_s_mem()); -> have tried this one too gives the same result
BUF_MEM *bptr = 0;

BIO_read_filename(certBio, "E:\\share\\Folder\\TempCert.pem");

x509 = PEM_read_bio_X509_AUX(certBio, NULL, pass_cb, NULL);

long res = BIO_get_mem_ptr(certBio, &bptr);
length = bptr->length;

memset(&buffer, 0, 1000);

int ret = BIO_read(certBio, &buffer, length);

BIO_free_all(certBio);
CertFreeCertificateContext(pContext);
CertCloseStore(hStore, 0);
return 0;
}

这是什么问题,

最佳答案

正如您在 The Man 上看到的那样bio_get_mem_ptr 想要一个memory BIO

A memory BIO is a source/sink BIO which uses memory for its I/O. Data written to a memory BIO is stored in a BUF_MEM structure which is extended as appropriate to accommodate the stored data.

您正在尝试使用一个文件,所以您应该使用 The Man 上的示例代码bio_s_file

关于c - BIO_get_mem_ptr 返回 0 作为长度和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42480322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com