11 std::ifstream file(filename, std::ios::binary | std::ios::ate);
12 std::vector<uint8_t> buffer;
14 if (!file.is_open()) {
15 std::cerr <<
"Error: Could not open file " << filename <<
" for reading." << std::endl;
20 std::streamsize size = file.tellg();
21 file.seekg(0, std::ios::beg);
27 if (!file.read(
reinterpret_cast<char*
>(buffer.data()), size)) {
28 std::cerr <<
"Error: Failed to read data from file " << filename << std::endl;
69 std::vector<string>* detectedRules = (std::vector<string>*)user_data;
72 yrx_rule_namespace(rule, &ns, &ns_len);
73 yrx_rule_identifier(rule, &ident, &ident_len);
75 detectedRules->push_back(std::string(ns, ns + ns_len) +
"::" +
76 std::string(ident, ident + ident_len));
84 std::vector<string> detectedRules;
86 if (g_yaraRules ==
nullptr) {
87 throw std::runtime_error(
"YARA rules not initialized");
90 YRX_SCANNER* rawScanner =
nullptr;
91 if (yrx_scanner_create(g_yaraRules, &rawScanner) != YRX_SUCCESS) {
92 throw std::runtime_error(
"Failed to create YARA scanner");
97 std::unique_ptr<YRX_SCANNER,
decltype(&yrx_scanner_destroy)> scanner(rawScanner, yrx_scanner_destroy);
99 if (yrx_scanner_on_matching_rule(scanner.get(),
matchingRule, &detectedRules) != YRX_SUCCESS) {
100 throw std::runtime_error(
"Failed to set matching rule callback");
104 if (scanBuffer.empty()) {
107 throw std::runtime_error(
"Failed to read file for scanning: " + file_path);
110 if (yrx_scanner_scan(scanner.get(), scanBuffer.data(), scanBuffer.size()) != YRX_SUCCESS) {
111 throw std::runtime_error(
"Failed to scan file: " + file_path);
114 return detectedRules;