Я пишу функцию на 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;
}
Хорошо. Я добавил функцию. Сообщение появляется там, где комментарий «показывает сообщение здесь».





Я думаю, вам нужно дать свое определение
url2file. Вы также должны указать, где встречается «Невозможно выделить память».