Changed the deprecated thumbnail_util APIs in msg-service
[platform/core/messaging/msg-service.git] / utils / MsgUtilFile.cpp
index c7e4b03..9b00350 100755 (executable)
@@ -26,8 +26,8 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <libgen.h>
+#include <dlfcn.h>
 
-#include <csr-content-screening.h>
 #include <thumbnail_util.h>
 #include <image_util.h>
 
@@ -44,41 +44,13 @@ extern "C" {
        #include <aul.h>
 }
 
+#define PATH_LIBCSR_CLIENT "/usr/lib/libcsr-client.so.2"
+#define THUMB_WIDTH 320
+#define THUMB_HEIGHT 240
+
 MsgMutex g_mx;
 MsgCndVar g_cv;
 
-void thumbnail_completed_cb(thumbnail_util_error_e error, const char *request_id,
-                                                                       int thumb_width, int thumb_height,
-                                                                       unsigned char *thumb_data, int thumb_size, void *user_data)
-{
-       MSG_BEGIN();
-
-       g_mx.lock();
-
-       if (!user_data) {
-               MSG_WARN("dstPath is NULL");
-               g_cv.signal();
-               g_mx.unlock();
-               return;
-       }
-
-       MSG_DEBUG("=================[RESULT]");
-       MSG_DEBUG("error_code [%d]", error);
-       MSG_DEBUG("request id [%s]", request_id);
-       MSG_DEBUG("width [%d], height [%d]", thumb_width, thumb_height);
-       MSG_DEBUG("size [%d]", thumb_size);
-
-       int ret = 0;
-       ret = image_util_encode_jpeg(thumb_data, thumb_width, thumb_height, IMAGE_UTIL_COLORSPACE_BGRA8888, 100, (char *)user_data);
-       if (ret != IMAGE_UTIL_ERROR_NONE)
-               MSG_WARN("image_util_encode_jpeg() is failed");
-
-       g_cv.signal();
-       g_mx.unlock();
-
-       MSG_END();
-}
-
 /*==================================================================================================
                                      FUNCTION IMPLEMENTATION
 ==================================================================================================*/
@@ -100,19 +72,16 @@ bool MakeThumbnail(char *srcPath, char *dstPath)
 
        int ret = THUMBNAIL_UTIL_ERROR_NONE;
        char *req_id = NULL;
-       thumbnail_h thumb_h;
-       thumbnail_util_create(&thumb_h);
-       thumbnail_util_set_path(thumb_h, srcPath);
 
-       ret = thumbnail_util_extract(thumb_h, thumbnail_completed_cb, dstPath, &req_id);
-       thumbnail_util_destroy(thumb_h);
+       ret = thumbnail_util_extract_to_file(srcPath, THUMB_WIDTH, THUMB_HEIGHT, dstPath);
+
        if (req_id) {
                g_free(req_id);
                req_id = NULL;
        }
 
        if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
-               MSG_ERR("thumbnail_util_extract is failed");
+               MSG_ERR("thumbnail_util_extract_to_file is failed");
                g_mx.unlock();
                return false;
        }
@@ -183,7 +152,7 @@ int MsgFseek(FILE *pFile, long int offset, int origin)
 
        int ret = -1;
 
-       MSG_DEBUG("[FILE] pFile [%p], offset [%d], origin [%d]", pFile, offset, origin);
+       MSG_DEBUG("[FILE] pFile [%p], offset [%ld], origin [%d]", pFile, offset, origin);
 
        try {
                ret = fseek(pFile, offset, origin); /* return 0, if success. */
@@ -204,7 +173,7 @@ size_t MsgWriteFile(const char *pData, size_t size, size_t count, FILE *pFile)
 
        size_t nWrite = 0;
 
-       MSG_DEBUG("[FILE] pData [%p], size [%d], count [%d], pFile [%p]", pData, size, count, pFile);
+       MSG_DEBUG("[FILE] pData [%p], size [%zu], count [%zu], pFile [%p]", pData, size, count, pFile);
 
        try {
                nWrite = fwrite(pData, size, count, pFile);
@@ -305,7 +274,7 @@ bool MsgCreateFileName(char *pFileName)
                /* Create Random Number */
                srandom((unsigned int)ts.tv_nsec);
 
-               MSG_DEBUG("ts.tv_nsec : %d", ts.tv_nsec);
+               MSG_DEBUG("ts.tv_nsec : %ld", ts.tv_nsec);
 
                /* between 1 - 1000000000 */
                snprintf(pFileName, MSG_FILENAME_LEN_MAX, "MSG_%lu.DATA", random()%1000000000+1);
@@ -321,7 +290,7 @@ bool MsgCreateFileName(char *pFileName)
 bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
 {
        if (!pFileName || !ppData || !pDataSize) {
-               MSG_ERR("Invalid params!! pFileName=%x, ppData=%x, pDataSize=%x", pFileName, ppData, pDataSize);
+               MSG_ERR("Invalid params");
                return false;
        }
 
@@ -671,9 +640,9 @@ bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int m
        }
 
        MSG_DEBUG("MsgWriteDataFromEncodeBuffer:");
-       MSG_DEBUG("pInBuffer %x", pInBuffer);
+       MSG_DEBUG("pInBuffer %s", pInBuffer);
        MSG_DEBUG("pPtr %d",  (*pPtr));
-       MSG_DEBUG("before to fwite %x", pFile);
+       MSG_DEBUG("before to fwite %p", pFile);
 
        if (MsgWriteFile(pInBuffer, sizeof(char), (*pPtr), pFile) != (size_t)(*pPtr)) {
                MSG_FATAL("MsgWriteFile failed");
@@ -1244,14 +1213,15 @@ void MsgGetMimeType(char *filePath, char *mimeType, int size)
        aul_get_mime_from_file(filePath, mimeType, size);
 }
 
-
 int MsgTcsScanFile(const char *filepath, int *bLevel)
 {
        MSG_BEGIN();
-       csr_cs_context_h csr_handle = NULL;
-       csr_cs_malware_h detected = NULL;
-       int ret = 0;
-       int ret_b_level = -1;
+
+       int (*_csr_cs_context_create)(void **handle);
+       int (*_csr_cs_scan_file)(void *handle, const char *filepath, void **malware);
+       int (*_csr_cs_context_destroy)(void *handle);
+       int (*_csr_cs_malware_get_severity)(void *malware, void *severity);
+       int (*_csr_cs_malware_get_name)(void *malware, char **name);
 
        if (MsgAccessFile(filepath, R_OK) == false) {
                MSG_SEC_DEBUG("not exist source file [%s]", filepath);
@@ -1260,51 +1230,81 @@ int MsgTcsScanFile(const char *filepath, int *bLevel)
 
        MSG_SEC_DEBUG("Scanning file name : %s\n", filepath);
 
-       ret = csr_cs_context_create(&csr_handle);
-       if (ret != CSR_ERROR_NONE) {
+       void *lib_handle = NULL;
+       lib_handle = dlopen(PATH_LIBCSR_CLIENT, RTLD_LAZY);
+       if (!lib_handle) {
+               MSG_ERR("Unable to open %s", PATH_LIBCSR_CLIENT);
+               return 0;
+       }
+
+       _csr_cs_context_create = (int(*)(void**))dlsym(lib_handle, "csr_cs_context_create");
+       _csr_cs_scan_file = (int(*)(void*, const char*, void**))dlsym(lib_handle, "csr_cs_scan_file");
+       _csr_cs_context_destroy = (int(*)(void*))dlsym(lib_handle, "csr_cs_context_destroy");
+       _csr_cs_malware_get_severity = (int(*)(void*, void*))dlsym(lib_handle, "csr_cs_malware_get_severity");
+       _csr_cs_malware_get_name = (int(*)(void*, char**))dlsym(lib_handle, "csr_cs_malware_get_name");
+
+       int ret = 0;
+       if (!_csr_cs_context_create || !_csr_cs_scan_file || !_csr_cs_context_destroy
+                       || !_csr_cs_malware_get_severity || !_csr_cs_malware_get_name) {
+               MSG_ERR("Failed to load CSR symbols");
+               if (lib_handle)
+                               dlclose(lib_handle);
+               return -1;
+       }
+
+       void *csr_handle = NULL;
+       ret = _csr_cs_context_create(&csr_handle);
+       if (ret != 0) {
                MSG_DEBUG("csr_cs_context_create error: err = %d\n", ret);
+               if (lib_handle)
+                               dlclose(lib_handle);
                return -1;
        }
 
-       ret = csr_cs_scan_file(csr_handle, filepath, &detected);
-       if (ret == CSR_ERROR_NONE) {
-               if (detected) {
-                       csr_cs_severity_level_e severity;
-                       char *name = NULL;
+       do {
+               void *detected = NULL;
+               ret = _csr_cs_scan_file(csr_handle, filepath, &detected);
+               if (ret != 0) {
+                       MSG_DEBUG("csr_cs_scan_file fail: err = %d\n", ret);
+                       break;
+               }
 
-                       ret = csr_cs_malware_get_severity(detected, &severity);
-                       if (ret != CSR_ERROR_NONE) {
-                               MSG_DEBUG("csr_cs_malware_get_severity error: err = %d\n", ret);
-                       }
+               if (NULL == detected) {
+                       MSG_DEBUG("Nothing detected");
+                       break;
+               }
 
-                       ret = csr_cs_malware_get_name(detected, &name);
-                       if (ret != CSR_ERROR_NONE) {
-                               MSG_DEBUG("csr_cs_malware_get_name error: err = %d\n", ret);
-                       }
+               int severity = 0x01; /* CSR_CS_SEVERITY_LOW */
+               ret = _csr_cs_malware_get_severity(detected, &severity);
+               if (ret != 0) {
+                       MSG_DEBUG("csr_cs_malware_get_severity error: err = %d\n", ret);
+               }
+               MSG_DEBUG(" +-- Malware Severity class: %d\n", severity);
 
-                       MSG_SEC_DEBUG(" +-- Malware Name: [%s]\n", name);
-                       MSG_DEBUG(" +-- Malware Severity class: %d\n", severity);
+               char *name = NULL;
+               ret = _csr_cs_malware_get_name(detected, &name);
+               if (ret != 0) {
+                       MSG_DEBUG("csr_cs_malware_get_name error: err = %d\n", ret);
+               }
+               MSG_SEC_DEBUG(" +-- Malware Name: [%s]\n", name);
 
-                       ret_b_level = (int)severity;
+               if (name)
+                       free(name);
 
-                       if (name) {
-                               free(name);
-                               name = NULL;
-                       }
-               }
-       } else {
-               MSG_DEBUG("csr_cs_scan_file fail: err = %d\n", ret);
-       }
+               if (bLevel)
+                       *bLevel = severity;
 
-       ret = csr_cs_context_destroy(csr_handle);
-       if (ret != CSR_ERROR_NONE) {
+       } while (0);
+
+       ret = _csr_cs_context_destroy(csr_handle);
+       if (ret != 0)
                MSG_DEBUG("csr_cs_context_destroy error: err = %d\n", ret);
-       }
 
-       if (bLevel)
-               *bLevel = ret_b_level;
+       if (lib_handle)
+               dlclose(lib_handle);
 
        MSG_END();
-
        return 0;
 }
+
+