FILE *decrypt(){
FILE *cryptedfile = fopen("file.ext", "rb");
//... my decrypter code
return (the decrypted file as FILE*).
}
那么,有一种方法可以执行类似“将char *转换为FILE *”的操作吗?
我尝试了许多不同的解决方案,但均未成功,我也尝试创建新的tmpfile(),但结果似乎无法正常工作,我也不想创建tmpfile,而只是将其保存在内存中。
感谢 :)
答案 0 :(得分:1)
解密数据后,您可以创建管道以将解密的数据馈入其中,然后可以返回管道的读取端以读取数据。
FILE *decrypt(){
FILE *cryptedfile = fopen("file.ext", "rb");
char *data;
int len;
// load decrypted data into "data" and length info "len"
int p[2];
if (pipe(p) == -1) {
perror("pipe failed");
return NULL;
}
int rval;
if ((rval = write(p[1], data, len)) == -1) {
perror("write failed");
close(p[0]);
close(p[1]);
return NULL;
} else if (rval < len) {
printf("write failed, wrote %d, expected %d\n", rval, len);
close(p[0]);
close(p[1]);
return NULL;
}
return fdopen(p[0], "r");
}
或者,您也可以使用fmemopen
:
FILE *decrypt(){
FILE *cryptedfile = fopen("file.ext", "rb");
char *data;
int len;
// load decrypted data into "data" and length info "len"
return fmemopen(data, len, "rb");
}