我正在用C语言编写一个函数,它将在Debian 8上运行。该函数的作用就是下载文件并使用cURL和ClamAV进行扫描。所以,我写了两个函数url2file()
和clamav_scan()
。我想将url2file()
插入clamav_scan()
。但是,它会显示错误消息'无法分配内存......为什么?如果我删除url2file()
中的行clamav_scan()
,则效果非常好。
以下是我写的代码。
谢谢。
// this function scans a file by using ClamAV
int clamav_scan(const char* file_name)
{
int ret = 0;
struct cl_engine* engine = NULL; // clamAV engine
unsigned int signo = 0; // clamAV signature number
const char* path = NULL; // clamAV DB path
const char* virname = NULL; // virus name
unsigned long int scanned = 0; // size of scanned data
char target[MAX_PATH] = "./"; // path of the scan file
strcat(target, file_name);
if ((ret = cl_init(CL_INIT_DEFAULT)) != CL_SUCCESS) {
printf("cl_init() failed: %s\n", cl_strerror(ret));
exit(1);
}
if ((engine = cl_engine_new()) == NULL) {
printf("cl_engine_new() failed\n");
exit(1);
}
if ((path = cl_retdbdir()) == NULL) {
printf("cl_retdbdir() failed\n");
cl_engine_free(engine);
exit(1);
}
if ((ret = cl_load(path, engine, &signo, CL_DB_STDOPT)) != CL_SUCCESS) {
printf("cl_load() failed: %s\n", cl_strerror(ret));
cl_engine_free(engine);
exit(1);
}
if ((ret = cl_engine_compile(engine)) != CL_SUCCESS) {
printf("cl_engine_compile() failed: %s\n", cl_strerror(ret));
cl_engine_free(engine);
exit(1);
}
// this is the inserted function I said
// this function downloads a file by using cURL
url2file(url, file_name);
printf("Scanning %s ...\n", target);
if ((ret = cl_scanfile((const char*)target, &virname, &scanned, engine, CL_SCAN_STDOPT)) == CL_VIRUS) {
printf("Virus detected: %s\n", virname);
}
else {
printf("No virus detected\n");
if (ret != CL_CLEAN)
printf("Error: %s\n", cl_strerror(ret)); // shows message here
}
printf("scanned: %lu\n", scanned);
cl_engine_free(engine);
return EXIT_SUCCESS;
}
我提到了
https://curl.haxx.se/libcurl/c/url2file.html
https://raw.githubusercontent.com/vrtadmin/clamav-faq/master/manual/clamdoc.pdf
+++添加了url2file()函数
static size_t write_data(void* ptr, size_t size, size_t nmemb, void* stream)
{
size_t written = fwrite(ptr, size, nmemb, (FILE*)stream);
return written;
}
int url2file(const char* url, const char* pagefilename)
{
CURL *curl_handle;
FILE *pagefile;
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* set URL to get here */
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
/* Switch on full protocol/debug output while testing */
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
/* disable progress meter, set to 0L to enable and disable debug output */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
/* open the file */
pagefile = fopen(pagefilename, "wb");
if (pagefile) {
/* write the page body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
/* get it! */
curl_easy_perform(curl_handle);
/* close the header file */
fclose(pagefile);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
return 0;
}