#include <list>
#include <fstream>
#include <iostream>
+#include <climits>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
if (logo_image == nullptr || image_size == nullptr)
return CSRE_ERROR_INVALID_PARAMETER;
+ auto s = eng->logoImage.size();
+ if (s > UINT_MAX - 1)
+ return CSRET_CS_ERROR_FILE_IO;
+
+ *image_size = reinterpret_cast<unsigned int>(s);
*logo_image = eng->logoImage.data();
- *image_size = eng->logoImage.size();
return CSRE_ERROR_NONE;
}
#include <list>
#include <fstream>
#include <iostream>
+#include <climits>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
if (vendor_logo_image == nullptr || image_size == nullptr)
return CSRE_ERROR_INVALID_PARAMETER;
+ auto s = eng->logoImage.size();
+ if (s > UINT_MAX - 1)
+ return CSRET_WP_ERROR_FILE_IO;
+
+ *image_size = reinterpret_cast<unsigned int>(s);
*vendor_logo_image = eng->logoImage.data();
- *image_size = eng->logoImage.size();
return CSRE_ERROR_NONE;
}
#include "client/utils.h"
#include "client/handle-ext.h"
#include "client/async-logic.h"
+#include "common/raw-buffer.h"
#include "common/cs-context.h"
#include "common/cs-detected.h"
#include "common/command-id.h"
case CSR_CS_USE_CORE_HALF:
case CSR_CS_USE_CORE_SINGLE:
return true;
+
default:
return false;
}
case CSR_CS_NOT_ASK_USER:
case CSR_CS_ASK_USER:
return true;
+
default:
return false;
}
}
+bool _isValid(const csr_cs_action_e &value)
+{
+ switch (value) {
+ case CSR_CS_ACTION_REMOVE:
+ case CSR_CS_ACTION_IGNORE:
+ case CSR_CS_ACTION_UNIGNORE:
+ return true;
+
+ default:
+ return false;
+ }
}
+template <class T>
+class CptrList {
+public:
+ CptrList(std::vector<T *> &_l) : l(_l) {}
+
+ ~CptrList()
+ {
+ for (auto &item : l)
+ delete item;
+ }
+
+ T *pop(void)
+ {
+ if (l.empty())
+ return nullptr;
+
+ auto iter = l.begin();
+ auto item = *iter;
+ l.erase(iter);
+
+ return item;
+ }
+
+private:
+ std::vector<T *> &l;
+};
+
+} // end of namespace
+
API
-int csr_cs_context_create(csr_cs_context_h* phandle)
+int csr_cs_context_create(csr_cs_context_h *phandle)
{
EXCEPTION_SAFE_START
return CSR_ERROR_INVALID_PARAMETER;
*phandle = reinterpret_cast<csr_cs_context_h>(
- new Client::HandleExt(std::shared_ptr<Context>(new CsContext())));
+ new Client::HandleExt(std::shared_ptr<Context>(new CsContext())));
return CSR_ERROR_NONE;
}
API
-int csr_cs_set_popup_message(csr_cs_context_h handle, const char* message)
+int csr_cs_set_popup_message(csr_cs_context_h handle, const char *message)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_scan_data(csr_cs_context_h handle, const unsigned char *data, unsigned int length, csr_cs_detected_h *pdetected)
+int csr_cs_scan_data(csr_cs_context_h handle, const unsigned char *data,
+ size_t length, csr_cs_detected_h *pdetected)
{
- (void) handle;
- (void) data;
- (void) length;
- (void) pdetected;
+ EXCEPTION_SAFE_START
+
+ if (handle == nullptr || pdetected == nullptr || data == nullptr)
+ return CSR_ERROR_INVALID_PARAMETER;
+
+ auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
+ auto ret = hExt->dispatch<std::pair<int, CsDetected *>>(
+ CommandId::SCAN_DATA,
+ hExt->getContext(),
+ RawBuffer(data, data + length));
+
+ if (ret.first != CSR_ERROR_NONE) {
+ ERROR("Error! ret: " << ret.first);
+ return ret.first;
+ }
+
+ if (ret.second == nullptr)
+ return CSR_ERROR_UNKNOWN; // deserialization logic error
+
+ if (ret.second->hasValue()) {
+ hExt->add(ret.second);
+ *pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
+ } else {
+ *pdetected = nullptr;
+ }
- DEBUG("start!");
return CSR_ERROR_NONE;
+
+ EXCEPTION_SAFE_END
}
API
-int csr_cs_scan_file(csr_cs_context_h handle, const char *file_path, csr_cs_detected_h *pdetected)
+int csr_cs_scan_file(csr_cs_context_h handle, const char *file_path,
+ csr_cs_detected_h *pdetected)
{
EXCEPTION_SAFE_START
if (handle == nullptr || pdetected == nullptr
- || file_path == nullptr || file_path[0] == '\0')
+ || file_path == nullptr || file_path[0] == '\0')
return CSR_ERROR_INVALID_PARAMETER;
auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
auto ret = hExt->dispatch<std::pair<int, CsDetected *>>(
- CommandId::SCAN_FILE,
- hExt->getContext(),
- std::string(file_path));
+ CommandId::SCAN_FILE,
+ hExt->getContext(),
+ std::string(file_path));
if (ret.first != CSR_ERROR_NONE) {
ERROR("Error! ret: " << ret.first);
}
API
-int csr_cs_set_callback_on_file_scanned(csr_cs_context_h handle, csr_cs_on_file_scanned_cb callback)
+int csr_cs_set_callback_on_file_scanned(csr_cs_context_h handle,
+ csr_cs_on_file_scanned_cb callback)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_set_callback_on_detected(csr_cs_context_h handle, csr_cs_on_detected_cb callback)
+int csr_cs_set_callback_on_detected(csr_cs_context_h handle,
+ csr_cs_on_detected_cb callback)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_set_callback_on_completed(csr_cs_context_h handle, csr_cs_on_completed_cb callback)
+int csr_cs_set_callback_on_completed(csr_cs_context_h handle,
+ csr_cs_on_completed_cb callback)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_set_callback_on_cancelled(csr_cs_context_h handle, csr_cs_on_cancelled_cb callback)
+int csr_cs_set_callback_on_cancelled(csr_cs_context_h handle,
+ csr_cs_on_cancelled_cb callback)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_set_callback_on_error(csr_cs_context_h handle, csr_cs_on_error_cb callback)
+int csr_cs_set_callback_on_error(csr_cs_context_h handle,
+ csr_cs_on_error_cb callback)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_scan_files_async(csr_cs_context_h handle, const char **file_paths, unsigned int count, void *user_data)
+int csr_cs_scan_files_async(csr_cs_context_h handle, const char **file_paths,
+ size_t count, void *user_data)
{
EXCEPTION_SAFE_START
auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
auto fileSet(std::make_shared<StrSet>());
- for (unsigned int i = 0; i < count; i++) {
+
+ for (size_t i = 0; i < count; i++) {
if (file_paths[i] == nullptr)
return CSR_ERROR_INVALID_PARAMETER;
hExt->dispatchAsync([hExt, user_data, fileSet] {
Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data,
- [&hExt] { return hExt->isStopped(); });
+ [&hExt] { return hExt->isStopped(); });
l.scanFiles(fileSet).second();
});
}
API
-int csr_cs_scan_dir_async(csr_cs_context_h handle, const char *dir_path, void *user_data)
+int csr_cs_scan_dir_async(csr_cs_context_h handle, const char *dir_path,
+ void *user_data)
{
EXCEPTION_SAFE_START
hExt->dispatchAsync([hExt, user_data, dir_path] {
Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data,
- [&hExt] { return hExt->isStopped(); });
+ [&hExt] { return hExt->isStopped(); });
l.scanDir(dir_path).second();
});
}
API
-int csr_cs_scan_dirs_async(csr_cs_context_h handle, const char **dir_paths, unsigned int count, void *user_data)
+int csr_cs_scan_dirs_async(csr_cs_context_h handle, const char **dir_paths,
+ size_t count, void *user_data)
{
EXCEPTION_SAFE_START
auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
auto dirSet(std::make_shared<StrSet>());
- for (unsigned int i = 0; i < count; i++) {
+
+ for (size_t i = 0; i < count; i++) {
if (dir_paths[i] == nullptr)
return CSR_ERROR_INVALID_PARAMETER;
hExt->dispatchAsync([hExt, user_data, dirSet] {
Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data,
- [&hExt] { return hExt->isStopped(); });
+ [&hExt] { return hExt->isStopped(); });
l.scanDirs(dirSet).second();
});
}
API
-int csr_cs_detected_get_severity(csr_cs_detected_h detected, csr_cs_severity_level_e* pseverity)
+int csr_cs_detected_get_severity(csr_cs_detected_h detected,
+ csr_cs_severity_level_e *pseverity)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_detected_get_threat_type(csr_cs_detected_h detected, csr_cs_threat_type_e* pthreat_type)
+int csr_cs_detected_get_threat_type(csr_cs_detected_h detected,
+ csr_cs_threat_type_e *pthreat_type)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_detected_get_malware_name(csr_cs_detected_h detected, const char** pmalware_name)
+int csr_cs_detected_get_malware_name(csr_cs_detected_h detected,
+ const char **pmalware_name)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_detected_get_detailed_url(csr_cs_detected_h detected, const char** pdetailed_url)
+int csr_cs_detected_get_detailed_url(csr_cs_detected_h detected,
+ const char **pdetailed_url)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_detected_get_timestamp(csr_cs_detected_h detected, time_t* ptimestamp)
+int csr_cs_detected_get_timestamp(csr_cs_detected_h detected,
+ time_t *ptimestamp)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_detected_get_file_name(csr_cs_detected_h detected, const char** pfile_name)
+int csr_cs_detected_get_file_name(csr_cs_detected_h detected,
+ const char **pfile_name)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_detected_get_user_response(csr_cs_detected_h detected, csr_cs_user_response_e* presponse)
+int csr_cs_detected_get_user_response(csr_cs_detected_h detected,
+ csr_cs_user_response_e *presponse)
{
EXCEPTION_SAFE_START
}
API
-int csr_cs_judge_detected_malware(csr_cs_context_h handle, const char *file_path, csr_cs_action_e action)
+int csr_cs_detected_is_app(csr_cs_detected_h detected, bool *pis_app)
{
- (void) handle;
- (void) file_path;
- (void) action;
+ EXCEPTION_SAFE_START
+
+ if (detected == nullptr || pis_app == nullptr)
+ return CSR_ERROR_INVALID_PARAMETER;
+
+ reinterpret_cast<Result *>(detected)->get(
+ static_cast<int>(CsDetected::Key::IsApp), *pis_app);
- DEBUG("start!");
return CSR_ERROR_NONE;
+
+ EXCEPTION_SAFE_END
}
API
-int csr_cs_get_detected_malware(csr_cs_context_h handle, const char *file_path, csr_cs_detected_h *pdetected)
+int csr_cs_detected_get_pkg_id(csr_cs_detected_h detected, const char **ppkg_id)
{
- (void) handle;
- (void) file_path;
- (void) pdetected;
+ EXCEPTION_SAFE_START
+
+ if (detected == nullptr || ppkg_id == nullptr)
+ return CSR_ERROR_INVALID_PARAMETER;
+
+ reinterpret_cast<Result *>(detected)->get(
+ static_cast<int>(CsDetected::Key::PkgId), ppkg_id);
- DEBUG("start!");
return CSR_ERROR_NONE;
+
+ EXCEPTION_SAFE_END
}
API
-int csr_cs_get_detected_malwares(csr_cs_context_h handle, const char *dir, csr_cs_detected_list_h *plist, int *pcount)
+int csr_cs_judge_detected_malware(csr_cs_context_h handle,
+ csr_cs_detected_h detected, csr_cs_action_e action)
{
- (void) handle;
- (void) dir;
- (void) plist;
- (void) pcount;
+ EXCEPTION_SAFE_START
+
+ if (handle == nullptr || detected == nullptr || !_isValid(action))
+ return CSR_ERROR_INVALID_PARAMETER;
+
+ auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
+ const char *file_path = nullptr;
+ reinterpret_cast<Result *>(detected)->get(
+ static_cast<int>(CsDetected::Key::TargetName), &file_path);
+ auto ret = hExt->dispatch<int>(
+ CommandId::JUDGE_STATUS,
+ hExt->getContext(),
+ std::string(file_path),
+ static_cast<int>(action));
+
+ if (ret != CSR_ERROR_NONE) {
+ ERROR("Error! ret: " << ret);
+ return ret;
+ }
- DEBUG("start!");
return CSR_ERROR_NONE;
+
+ EXCEPTION_SAFE_END
}
API
-int csr_cs_get_ignored_malware(csr_cs_context_h handle, const char *file_path, csr_cs_detected_h *pdetected)
+int csr_cs_get_detected_malware(csr_cs_context_h handle, const char *file_path,
+ csr_cs_detected_h *pdetected)
{
- (void) handle;
- (void) file_path;
- (void) pdetected;
+ EXCEPTION_SAFE_START
+
+ if (handle == nullptr || file_path == nullptr || pdetected == nullptr)
+ return CSR_ERROR_INVALID_PARAMETER;
+
+ auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
+ auto ret = hExt->dispatch<std::pair<int, CsDetected *>>(
+ CommandId::GET_DETECTED,
+ hExt->getContext(),
+ std::string(file_path));
+
+ if (ret.first != CSR_ERROR_NONE) {
+ ERROR("Error! ret: " << ret.first);
+ return ret.first;
+ }
+
+ if (ret.second == nullptr)
+ return CSR_ERROR_UNKNOWN; // deserialization logic error
+
+ if (ret.second->hasValue()) {
+ hExt->add(ret.second);
+ *pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
+ } else {
+ *pdetected = nullptr;
+ delete ret.second;
+ }
- DEBUG("start!");
return CSR_ERROR_NONE;
+
+ EXCEPTION_SAFE_END
+}
+
+API
+int csr_cs_get_detected_malwares(csr_cs_context_h handle, const char *dir,
+ csr_cs_detected_list_h *plist, size_t *pcount)
+{
+ EXCEPTION_SAFE_START
+
+ if (handle == nullptr || dir == nullptr
+ || plist == nullptr || pcount == nullptr)
+ return CSR_ERROR_INVALID_PARAMETER;
+
+ auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
+ auto ret = hExt->dispatch<std::pair<int, std::vector<CsDetected *>>>(
+ CommandId::GET_DETECTED_LIST,
+ hExt->getContext(),
+ std::string(dir));
+
+ if (ret.first != CSR_ERROR_NONE) {
+ ERROR("Error! ret: " << ret.first);
+ return ret.first;
+ }
+
+ if (ret.second.empty()) {
+ *plist = nullptr;
+ *pcount = 0;
+ return CSR_ERROR_NONE;
+ }
+
+ CptrList<CsDetected> cptrList(ret.second);
+
+ ResultListPtr resultListPtr(new ResultList);
+
+ while (auto dptr = cptrList.pop())
+ resultListPtr->emplace_back(std::unique_ptr<Result>(dptr));
+
+ *plist = reinterpret_cast<csr_cs_detected_list_h>(resultListPtr.get());
+ *pcount = resultListPtr->size();
+
+ hExt->add(std::move(resultListPtr));
+
+ return CSR_ERROR_NONE;
+
+ EXCEPTION_SAFE_END
+}
+
+API
+int csr_cs_get_ignored_malware(csr_cs_context_h handle, const char *file_path,
+ csr_cs_detected_h *pdetected)
+{
+ EXCEPTION_SAFE_START
+
+ if (handle == nullptr || file_path == nullptr || pdetected == nullptr)
+ return CSR_ERROR_INVALID_PARAMETER;
+
+ auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
+ auto ret = hExt->dispatch<std::pair<int, CsDetected *>>(
+ CommandId::GET_IGNORED,
+ hExt->getContext(),
+ std::string(file_path));
+
+ if (ret.first != CSR_ERROR_NONE) {
+ ERROR("Error! ret: " << ret.first);
+ return ret.first;
+ }
+
+ if (ret.second == nullptr)
+ return CSR_ERROR_UNKNOWN; // deserialization logic error
+
+ if (ret.second->hasValue()) {
+ hExt->add(ret.second);
+ *pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
+ } else {
+ *pdetected = nullptr;
+ delete ret.second;
+ }
+
+ return CSR_ERROR_NONE;
+
+ EXCEPTION_SAFE_END
}
API
-int csr_cs_get_ignored_malwares(csr_cs_context_h handle, const char *dir, csr_cs_detected_list_h *plist, int *pcount)
+int csr_cs_get_ignored_malwares(csr_cs_context_h handle, const char *dir,
+ csr_cs_detected_list_h *plist, size_t *pcount)
{
- (void) handle;
- (void) dir;
- (void) plist;
- (void) pcount;
+ EXCEPTION_SAFE_START
+
+ if (handle == nullptr || dir == nullptr
+ || plist == nullptr || pcount == nullptr)
+ return CSR_ERROR_INVALID_PARAMETER;
+
+ auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
+ auto ret = hExt->dispatch<std::pair<int, std::vector<CsDetected *>>>(
+ CommandId::GET_IGNORED_LIST,
+ hExt->getContext(),
+ std::string(dir));
+
+ if (ret.first != CSR_ERROR_NONE) {
+ ERROR("Error! ret: " << ret.first);
+ return ret.first;
+ }
+
+ if (ret.second.empty()) {
+ *plist = nullptr;
+ *pcount = 0;
+ return CSR_ERROR_NONE;
+ }
+
+ CptrList<CsDetected> cptrList(ret.second);
+
+ ResultListPtr resultListPtr(new ResultList);
+
+ while (auto dptr = cptrList.pop())
+ resultListPtr->emplace_back(std::unique_ptr<Result>(dptr));
+
+ *plist = reinterpret_cast<csr_cs_detected_list_h>(resultListPtr.get());
+ *pcount = resultListPtr->size();
+
+ hExt->add(std::move(resultListPtr));
- DEBUG("start!");
return CSR_ERROR_NONE;
+
+ EXCEPTION_SAFE_END
}
API
-int csr_cs_dlist_get_detected(csr_cs_detected_list_h list, int index, csr_cs_detected_h *pdetected)
+int csr_cs_dlist_get_detected(csr_cs_detected_list_h list, size_t index,
+ csr_cs_detected_h *pdetected)
{
- (void) list;
- (void) index;
- (void) pdetected;
+ EXCEPTION_SAFE_START
+
+ if (list == nullptr || pdetected == nullptr)
+ return CSR_ERROR_INVALID_PARAMETER;
+
+ auto dListPtr = reinterpret_cast<ResultList *>(list);
+
+ if (index >= dListPtr->size())
+ return CSR_ERROR_INVALID_PARAMETER;
+
+ *pdetected = reinterpret_cast<csr_cs_detected_h>(dListPtr->at(index).get());
- DEBUG("start!");
return CSR_ERROR_NONE;
+
+ EXCEPTION_SAFE_END
}
#include "client/handle.h"
#include <stdexcept>
+#include <utility>
namespace Csr {
namespace Client {
m_ctx->add(result);
}
+void Handle::add(ResultListPtr &&resultListPtr)
+{
+ if (resultListPtr == nullptr)
+ throw std::logic_error("result list pointer shouldn't be null");
+
+ m_ctx->add(std::forward<ResultListPtr>(resultListPtr));
+}
+
} // namespace Client
} // namespace Csr
Type dispatch(Args &&...);
void add(Result *);
+ void add(ResultListPtr &&);
std::shared_ptr<Context> &getContext(void) noexcept;
namespace Csr {
enum class CommandId : int {
- SCAN_FILE = 0x01,
- JUDGE_STATUS = 0x02,
- CHECK_URL = 0x03,
- DIR_GET_RESULTS = 0x04,
- DIR_GET_FILES = 0x05
+ // content scanning
+ // scanning
+ SCAN_DATA = 0x1001,
+ SCAN_FILE = 0x1002,
+ // delta, history
+ DIR_GET_RESULTS = 0x1101,
+ DIR_GET_FILES = 0x1102,
+ GET_DETECTED = 0x1103,
+ GET_DETECTED_LIST = 0x1104,
+ GET_IGNORED = 0x1105,
+ GET_IGNORED_LIST = 0x1106,
+ // handle result
+ JUDGE_STATUS = 0x1201,
+
+ // web protection
+ CHECK_URL = 0x2001
};
}
m_severity(CSR_CS_SEVERITY_LOW),
m_threat(CSR_CS_THREAT_GENERIC),
m_response(CSR_CS_NO_ASK_USER),
+ m_isApp(false),
m_ts(0)
{
}
setValueFlag();
}
+void CsDetected::set(int key, bool value)
+{
+ switch (static_cast<Key>(key)) {
+ case Key::IsApp:
+ m_isApp = value;
+ break;
+
+ default:
+ throw std::logic_error(FORMAT("Invalid key[" << key
+ << "] comes in to set as bool."));
+ }
+
+ setValueFlag();
+}
+
void CsDetected::set(int key, const std::string &value)
{
switch (static_cast<Key>(key)) {
m_detailedUrl = value;
break;
+ case Key::PkgId:
+ m_pkgId = value;
+ break;
+
default:
throw std::logic_error(FORMAT("Invalid key[" << key
<< "] comes in to set as string."));
}
}
+void CsDetected::get(int key, bool &value) const
+{
+ switch (static_cast<Key>(key)) {
+ case Key::IsApp:
+ value = m_isApp;
+ break;
+
+ default:
+ throw std::logic_error(FORMAT("Invalid key[" << key
+ << "] comes in to get as bool."));
+ }
+}
+
void CsDetected::get(int key, const char **value) const
{
if (value == nullptr)
case Key::DetailedUrl:
*value = m_detailedUrl.c_str();
+ break;
+
+ case Key::PkgId:
+ *value = m_pkgId.c_str();
+ break;
default:
throw std::logic_error(FORMAT("Invalid key[" << key
namespace Csr {
+class CsDetected;
+using CsDetectedPtr = std::unique_ptr<CsDetected>;
+using CsDetectedList = std::vector<CsDetectedPtr>;
+
class CsDetected : public Result {
public:
// key for set/get
TargetName = 0x01, // string
MalwareName = 0x02, // string
DetailedUrl = 0x03, // string
+ PkgId = 0x04, // string
Severity = 0x10, // int
Threat = 0x11, // int
UserResponse = 0x12, // int
- TimeStamp = 0x21 // time_t
+ TimeStamp = 0x20, // time_t
+
+ IsApp = 0x30 // bool
};
CsDetected();
CsDetected &operator=(CsDetected &&);
virtual void set(int, int) override;
+ virtual void set(int, bool) override;
virtual void set(int, const std::string &) override;
virtual void set(int, const char *) override;
virtual void set(int, time_t) override;
virtual void get(int, int &) const override;
+ virtual void get(int, bool &) const override;
virtual void get(int, const char **) const override;
virtual void get(int, time_t &) const override;
std::string m_malwareName;
std::string m_detailedUrl;
+ std::string m_pkgId;
csr_cs_severity_level_e m_severity;
csr_cs_threat_type_e m_threat;
csr_cs_user_response_e m_response;
+ bool m_isApp;
time_t m_ts;
};
return *this;
}
-void Context::add(std::unique_ptr<Result> &&item)
+void Context::add(ResultPtr &&item)
{
std::lock_guard<std::mutex> l(m_mutex);
- m_results.emplace_back(std::forward<std::unique_ptr<Result>>(item));
+ m_results.emplace_back(std::forward<ResultPtr>(item));
}
void Context::add(Result *item)
m_results.emplace_back(item);
}
+void Context::add(ResultListPtr &&item)
+{
+ std::lock_guard<std::mutex> l(m_mutex);
+ m_resultLists.emplace_back(std::forward<ResultListPtr>(item));
+}
+
size_t Context::size() const
{
std::lock_guard<std::mutex> l(m_mutex);
bool m_hasVal;
};
+using ResultPtr = std::unique_ptr<Result>;
+using ResultList = std::vector<ResultPtr>;
+using ResultListPtr = std::unique_ptr<ResultList>;
+
class Context : public ISerializable, public KvpContainer {
public:
Context();
Context(const Context &);
Context &operator=(const Context &);
- void add(std::unique_ptr<Result> &&);
+ void add(ResultPtr &&);
void add(Result *);
+ void add(ResultListPtr &&);
size_t size(void) const;
// for destroying with context
- std::vector<std::unique_ptr<Result>> m_results;
+ std::vector<ResultPtr> m_results;
+ std::vector<ResultListPtr> m_resultLists;
private:
mutable std::mutex m_mutex;
#pragma once
#include <ctime>
-#include <cstddef>
#include <vector>
#include <string>
INFO("Request dispatch! CommandId: " << static_cast<int>(info.first));
switch (info.first) {
+ case CommandId::SCAN_DATA: {
+ CsContext context;
+ RawBuffer data;
+ info.second.Deserialize(context, data);
+ return scanData(context, data);
+ }
+
case CommandId::SCAN_FILE: {
CsContext context;
std::string filepath;
return dirGetFiles(context, dir);
}
+ case CommandId::JUDGE_STATUS: {
+ CsContext context;
+ std::string filepath;
+ info.second.Deserialize(context, filepath);
+ return judgeStatus(context, filepath);
+ }
+
+ case CommandId::GET_DETECTED: {
+ CsContext context;
+ std::string filepath;
+ info.second.Deserialize(context, filepath);
+ return getDetected(context, filepath);
+ }
+
+ case CommandId::GET_DETECTED_LIST: {
+ CsContext context;
+ std::string dir;
+ info.second.Deserialize(context, dir);
+ //return getDetectedList(context, dir);
+ RawBuffer buff = getDetectedList(context, dir);
+ INFO(" CommandId::GET_DETECTED_LIST : RawBuffer Size[" << buff.size() << "]");
+ return buff;
+ }
+
+ case CommandId::GET_IGNORED: {
+ CsContext context;
+ std::string filepath;
+ info.second.Deserialize(context, filepath);
+ return getIgnored(context, filepath);
+ }
+
+ case CommandId::GET_IGNORED_LIST: {
+ CsContext context;
+ std::string dir;
+ info.second.Deserialize(context, dir);
+ return getIgnoredList(context, dir);
+ }
+
+
default:
throw std::range_error(FORMAT("Command id[" << static_cast<int>(info.first)
<< "] isn't in range."));
return std::make_pair(id, std::move(q));
}
+RawBuffer Logic::scanData(const CsContext &context, const RawBuffer &data)
+{
+ INFO("Scan Data[size=" << data.size() << "] by engine");
+
+ printCsContext(context);
+
+ return BinaryQueue::Serialize(CSR_ERROR_NONE, CsDetected()).pop();
+}
+
RawBuffer Logic::scanFile(const CsContext &context, const std::string &filepath)
{
INFO("Scan file[" << filepath << "] by engine");
return BinaryQueue::Serialize(CSR_ERROR_NONE, StrSet()).pop();
}
+RawBuffer Logic::judgeStatus(const CsContext &context, const std::string &filepath)
+{
+ INFO("Judge Status[" << filepath << "] by engine");
+
+ printCsContext(context);
+
+ return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
+}
+
+RawBuffer Logic::getDetected(const CsContext &context, const std::string &filepath)
+{
+ INFO("Get Detected[" << filepath << "] by engine");
+
+ printCsContext(context);
+
+ CsDetected detected;
+ detected.set(static_cast<int>(CsDetected::Key::TargetName), "test_file");
+
+ return BinaryQueue::Serialize(CSR_ERROR_NONE, detected).pop();
+}
+
+RawBuffer Logic::getDetectedList(const CsContext &context, const std::string &dir)
+{
+ INFO("Get Detected List[" << dir << "] by engine");
+
+ printCsContext(context);
+
+ CsDetectedPtr detected(new CsDetected());
+ detected->set(static_cast<int>(CsDetected::Key::TargetName), "test_file");
+
+ CsDetectedList list;
+ list.emplace_back(std::move(detected));
+
+ return BinaryQueue::Serialize(CSR_ERROR_NONE, list).pop();
+}
+
+RawBuffer Logic::getIgnored(const CsContext &context, const std::string &filepath)
+{
+ INFO("Get Ignored[" << filepath << "] by engine");
+
+ printCsContext(context);
+
+ CsDetected detected;
+ detected.set(static_cast<int>(CsDetected::Key::TargetName), "test_file");
+
+ return BinaryQueue::Serialize(CSR_ERROR_NONE, detected).pop();
+}
+
+RawBuffer Logic::getIgnoredList(const CsContext &context, const std::string &dir)
+{
+ INFO("Get Ignored List[" << dir << "] by engine");
+
+ printCsContext(context);
+
+ CsDetectedPtr detected(new CsDetected());
+ detected->set(static_cast<int>(CsDetected::Key::TargetName), "test_file");
+
+ CsDetectedList list;
+ list.emplace_back(std::move(detected));
+
+ return BinaryQueue::Serialize(CSR_ERROR_NONE, list).pop();
+}
+
RawBuffer Logic::checkUrl(const WpContext &context, const std::string &url)
{
INFO("Check url[" << url << "] by engine");
private:
std::pair<CommandId, BinaryQueue> getRequestInfo(const RawBuffer &);
+ RawBuffer scanData(const CsContext &context, const RawBuffer &data);
RawBuffer scanFile(const CsContext &context, const std::string &filepath);
RawBuffer dirGetResults(const CsContext &context, const std::string &dir);
RawBuffer dirGetFiles(const CsContext &context, const std::string &dir);
+ RawBuffer judgeStatus(const CsContext &context, const std::string &filepath);
+ RawBuffer getDetected(const CsContext &context, const std::string &filepath);
+ RawBuffer getDetectedList(const CsContext &context, const std::string &dir);
+ RawBuffer getIgnored(const CsContext &context, const std::string &filepath);
+ RawBuffer getIgnoredList(const CsContext &context, const std::string &dir);
+
RawBuffer checkUrl(const WpContext &context, const std::string &url);
};
* @version 1.0
* @brief
*/
-
#pragma once
#include <ctime>
-#include <cstddef>
#include <vector>
#include <string>
-
#include "csre/web-protection-types.h"
#include "csre/web-protection-engine-info.h"
#define __CSR_CONTENT_SCREENING_API_H_
#include <time.h>
+#include <stdbool.h>
+#include <stddef.h>
#include "csr/content-screening-types.h"
#include "csr/error.h"
*/
int csr_cs_scan_data(csr_cs_context_h handle,
const unsigned char *data,
- unsigned int length,
+ size_t length,
csr_cs_detected_h *pdetected);
/**
*/
int csr_cs_scan_files_async(csr_cs_context_h handle,
const char **file_paths,
- unsigned int count,
+ size_t count,
void *user_data);
/**
* @retval #CSR_ERROR_NONE Successful
* @retval #CSR_ERROR_INVALID_HANDLE Invalid handle
* @retval #CSR_ERROR_OUT_OF_MEMORY Not enough memory
- * @retval #CSR_ERROR_INVALID_PARAMETER file_paths or count is invalid
+ * @retval #CSR_ERROR_INVALID_PARAMETER dir_paths or count is invalid
* @retval #CSR_ERROR_PERMISSION_DENIED Access denied
* @retval #CSR_ERROR_FILE_NOT_FOUND File not found
* @retval #CSR_ERROR_SOCKET Socket error between client and server
*/
int csr_cs_scan_dirs_async(csr_cs_context_h handle,
const char **dir_paths,
- unsigned int count,
+ size_t count,
void *user_data);
* @retval #CSR_ERROR_INVALID_HANDLE Invalid result handle
* @retval #CSR_ERROR_INVALID_PARAMETER presponse is invalid
* @retval #CSR_ERROR_UNKNOWN Error with unknown reason
- *
*/
int csr_cs_detected_get_user_response(csr_cs_detected_h detected, csr_cs_user_response_e* presponse);
/**
+ * @brief check if a malware was detected in an application or in a file.
+ *
+ * @param[in] detected A detected malware handle.
+ * @param[out] pis_app A pointer of a flag indicating the position a malware was detected.
+ *
+ * @return #CSR_ERROR_NONE on success, otherwise a negative error value
+ *
+ * @retval #CSR_ERROR_NONE Successful
+ * @retval #CSR_ERROR_INVALID_HANDLE Invalid result handle
+ * @retval #CSR_ERROR_INVALID_PARAMETER pis_app is invalid
+ * @retval #CSR_ERROR_UNKNOWN Error with unknown reason
+ */
+int csr_cs_detected_is_app(csr_cs_detected_h detected, bool* pis_app);
+
+/**
+ * @brief extracts the package id of an application where a malware is detected from detected malware handle.
+ *
+ * @param[in] detected A detected malware handle.
+ * @param[out] ppkg_id A pointer of the pakcage id where a malware is detected. A caller should not free this string. This is a null when a malware was not detected in an application.
+ *
+ * @return #CSR_ERROR_NONE on success, otherwise a negative error value
+ *
+ * @retval #CSR_ERROR_NONE Successful
+ * @retval #CSR_ERROR_INVALID_HANDLE Invalid detected malware handle
+ * @retval #CSR_ERROR_INVALID_PARAMETER ppkg_id is invalid
+ * @retval #CSR_ERROR_UNKNOWN Error with unknown reason
+ */
+int csr_cs_detected_get_pkg_id(csr_cs_detected_h detected, const char** ppkg_id);
+
+/**
* @brief Judges how a detected malware file is handled.
*
* @details A detected malware may be removed or ignored. When action is
* again.
*
* @param[in] handle CSR CS context handle returned by csr_cs_context_create().
- * @param[in] file_path A path of a detected malware file.
+ * @param[in] detected A handle of a detected malware.
* @param[in] action An action to be taken.
*
* @return #CSR_ERROR_NONE on success, otherwise a negative error value
* @retval #CSR_ERROR_NONE Successful
* @retval #CSR_ERROR_INVALID_HANDLE Invalid handle
* @retval #CSR_ERROR_OUT_OF_MEMORY Not enough memory
- * @retval #CSR_ERROR_INVALID_PARAMETER file_path or action is invalid
+ * @retval #CSR_ERROR_INVALID_PARAMETER detected or action is invalid
* @retval #CSR_ERROR_PERMISSION_DENIED No permission to remove
* @retval #CSR_ERROR_FILE_NOT_FOUND File to take action on not found
* @retval #CSR_ERROR_SOCKET Socket error between client and server
* @retval #CSR_ERROR_SERVER Server has been failed for some reason
* @retval #CSR_ERROR_UNKNOWN Error with unknown reason
*/
-int csr_cs_judge_detected_malware(csr_cs_context_h handle, const char *file_path, csr_cs_action_e action);
+int csr_cs_judge_detected_malware(csr_cs_context_h handle, csr_cs_detected_h detected, csr_cs_action_e action);
/**
* @retval #CSR_ERROR_UNKNOWN Error with unknown reason
*/
int csr_cs_get_detected_malwares(csr_cs_context_h handle, const char *dir,
- csr_cs_detected_list_h *plist, int *pcount);
+ csr_cs_detected_list_h *plist, size_t *pcount);
/**
* @brief Gets information on a ignored malware file specified by file path.
* @retval #CSR_ERROR_UNKNOWN Error with unknown reason
*/
int csr_cs_get_ignored_malwares(csr_cs_context_h handle, const char *dir,
- csr_cs_detected_list_h *plist, int *pcount);
+ csr_cs_detected_list_h *plist, size_t *pcount);
/**
* @brief Extracts the detected malware handle from the detected malware list handle.
* @retval #CSR_ERROR_INVALID_PARAMETER index or pdetected is invalid.
* @retval #CSR_ERROR_UNKNOWN Error with unknown reason
*/
-int csr_cs_dlist_get_detected(csr_cs_detected_list_h list, int index, csr_cs_detected_h *pdetected);
+int csr_cs_dlist_get_detected(csr_cs_detected_list_h list, size_t index, csr_cs_detected_h *pdetected);
#ifdef __cplusplus
}
EXCEPTION_GUARD_END
}
+BOOST_AUTO_TEST_CASE(scan_data)
+{
+ EXCEPTION_GUARD_START
+
+ auto c = Test::Context<csr_cs_context_h>();
+ auto context = c.get();
+ csr_cs_detected_h detected;
+ unsigned char data[100] = {0, };
+
+ ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), &detected),
+ CSR_ERROR_NONE);
+
+ // no malware detected
+ CHECK_IS_NULL(detected);
+
+ EXCEPTION_GUARD_END
+}
+
BOOST_AUTO_TEST_CASE(scan_file)
{
EXCEPTION_GUARD_START
EXCEPTION_GUARD_END
}
+BOOST_AUTO_TEST_CASE(get_detected_malware)
+{
+ EXCEPTION_GUARD_START
+
+ auto c = Test::Context<csr_cs_context_h>();
+ auto context = c.get();
+ csr_cs_detected_h detected;
+
+ ASSERT_IF(csr_cs_get_detected_malware(context, "dummy_file_path", &detected),
+ CSR_ERROR_NONE);
+
+ // no malware detected
+ CHECK_IS_NOT_NULL(detected);
+
+ EXCEPTION_GUARD_END
+}
+
+BOOST_AUTO_TEST_CASE(get_detected_malwares)
+{
+ EXCEPTION_GUARD_START
+
+ auto c = Test::Context<csr_cs_context_h>();
+ auto context = c.get();
+ csr_cs_detected_list_h detected_list;
+ size_t cnt = 0;
+
+ ASSERT_IF(csr_cs_get_detected_malwares(context, "dummy_dir_path", &detected_list, &cnt),
+ CSR_ERROR_NONE);
+
+ // no malware detected
+ CHECK_IS_NOT_NULL(detected_list);
+
+ ASSERT_IF(cnt, static_cast<size_t>(1));
+
+ EXCEPTION_GUARD_END
+}
+
+BOOST_AUTO_TEST_CASE(get_ignored_malware)
+{
+ EXCEPTION_GUARD_START
+
+ auto c = Test::Context<csr_cs_context_h>();
+ auto context = c.get();
+ csr_cs_detected_h ignored;
+
+ ASSERT_IF(csr_cs_get_ignored_malware(context, "dummy_file_path", &ignored),
+ CSR_ERROR_NONE);
+
+ CHECK_IS_NOT_NULL(ignored);
+
+ EXCEPTION_GUARD_END
+}
+
+BOOST_AUTO_TEST_CASE(get_ignored_malwares)
+{
+ EXCEPTION_GUARD_START
+
+ auto c = Test::Context<csr_cs_context_h>();
+ auto context = c.get();
+ csr_cs_detected_list_h ignored_list;
+ size_t cnt = 0;
+
+ ASSERT_IF(csr_cs_get_ignored_malwares(context, "dummy_dir_path", &ignored_list, &cnt),
+ CSR_ERROR_NONE);
+
+ CHECK_IS_NOT_NULL(ignored_list);
+
+ ASSERT_IF(cnt, static_cast<size_t>(1));
+
+ EXCEPTION_GUARD_END
+}
+
+BOOST_AUTO_TEST_CASE(judge_detected_malware)
+{
+ EXCEPTION_GUARD_START
+
+ auto c = Test::Context<csr_cs_context_h>();
+ auto context = c.get();
+ csr_cs_detected_h detected;
+
+ ASSERT_IF(csr_cs_get_detected_malware(context, "dummy_file_path", &detected),
+ CSR_ERROR_NONE);
+ CHECK_IS_NOT_NULL(detected);
+
+ ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE),
+ CSR_ERROR_NONE);
+
+ EXCEPTION_GUARD_END
+}
+
BOOST_AUTO_TEST_SUITE_END()
namespace Test {
template <typename T, typename U>
-void _assert(T value, U expected, const std::string &filename, const std::string &funcname, unsigned int line)
+void _assert(const T &value, const U &expected, const std::string &filename, const std::string &funcname, unsigned int line)
{
BOOST_REQUIRE_MESSAGE(value == expected,
"[" << filename << " > " << funcname << " : " << line << "]"