Changed the deprecated thumbnail_util APIs in msg-service
[platform/core/messaging/msg-service.git] / utils / MsgUtilFile.cpp
index 5aa59dd..9b00350 100755 (executable)
 #include <unistd.h>
 #include <fcntl.h>
 #include <libgen.h>
+#include <dlfcn.h>
 
 #include <thumbnail_util.h>
 #include <image_util.h>
-#include <TCSImpl.h>
-#include <TCSErrorCodes.h>
 
 #include "MsgStorageTypes.h"
 #include "MsgDebug.h"
@@ -45,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("raw_data [0x%x], size [%d]", *thumb_data, 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
 ==================================================================================================*/
@@ -101,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;
        }
@@ -184,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. */
@@ -205,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);
@@ -306,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);
@@ -322,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;
        }
 
@@ -335,6 +303,16 @@ bool MsgOpenAndReadFile(const char *pFileName, char **ppData, int *pDataSize)
        snprintf(fullPath, MAX_FULL_PATH_SIZE, "%s%s", MSG_IPC_DATA_PATH, pFileName);
        MSG_SEC_DEBUG("open file name: %s", fullPath);
 
+       struct stat st;
+       if (stat(fullPath, &st) != 0) {
+               MSG_SEC_ERR("stat(%s, &st) != 0", fullPath);
+               return false;
+       }
+       if (S_ISDIR(st.st_mode)) {
+               MSG_ERR("S_ISDIR(st.st_mode)");
+               return false;
+       }
+
        pFile = MsgOpenFile(fullPath, "rb");
 
        if (pFile == NULL) {
@@ -662,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");
@@ -795,7 +773,6 @@ __CATCH:
 int MsgRmRf(char *pDirPath)
 {
        struct dirent *d = NULL;
-       struct dirent entry;
        DIR *dir;
 
        dir = opendir(pDirPath);
@@ -817,7 +794,8 @@ int MsgRmRf(char *pDirPath)
 
        bzero(path, size);
 
-       for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
+       errno = 0;
+       while ((d = readdir(dir)) != NULL) {
                if (d->d_type == DT_DIR) {
                        snprintf(path, size, "%s/%s", pDirPath, d->d_name);
 
@@ -861,6 +839,9 @@ int MsgRmRf(char *pDirPath)
        if (path != NULL)
                free(path);
 
+       if (errno != 0)
+               return -1;
+
        return 0;
 }
 
@@ -882,7 +863,6 @@ int MsgGetFileSize(const char *pFileName)
 unsigned int MsgDu(const char *pDirPath)
 {
        struct dirent *d = NULL;
-       struct dirent entry;
        DIR *dir;
 
        dir = opendir(pDirPath);
@@ -903,7 +883,8 @@ unsigned int MsgDu(const char *pDirPath)
 
        unsigned int totalFileSize = 0;
 
-       for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
+       errno = 0;
+       while ((d = readdir(dir)) != NULL) {
                if(d->d_type == DT_DIR) {
                        snprintf(path, size, "%s/%s", pDirPath, d->d_name);
 
@@ -940,6 +921,9 @@ unsigned int MsgDu(const char *pDirPath)
 
        free(path);
 
+       if (errno != 0)
+               return 0;
+
        return totalFileSize;
 }
 
@@ -983,7 +967,6 @@ bool MsgAppendFile(const char *pFilePath, const char *pData, int DataSize)
 void MsgMmsInitDir()
 {
        struct dirent *d = NULL;
-       struct dirent entry;
        DIR* dir = NULL;
 
        dir = opendir(MSG_DATA_PATH);
@@ -994,7 +977,7 @@ void MsgMmsInitDir()
        }
 
        /* Remove temporal Mms folder */
-       for (readdir_r(dir, &entry, &d); d != NULL; readdir_r(dir, &entry, &d)) {
+       while ((d = readdir(dir)) != NULL) {
                if (d->d_type == DT_DIR) {
                        if ((strcmp(".", d->d_name) == 0) || (strcmp("..", d->d_name) == 0))
                                continue;
@@ -1230,15 +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();
-       TCSLIB_HANDLE hLib;
-       TCSScanResult result;
-       TCSDetected* pDetected;
-       int rtn, i;
-       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);
@@ -1247,50 +1230,81 @@ int MsgTcsScanFile(const char *filepath, int *bLevel)
 
        MSG_SEC_DEBUG("Scanning file name : %s\n", filepath);
 
-       hLib = TCSLibraryOpen();
-       if(hLib == INVALID_TCSLIB_HANDLE) {
-               MSG_DEBUG("TCSLibraryOpen error\n");
+       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;
        }
 
-       rtn = TCSScanFile(hLib, filepath, TCS_DTYPE_UNKNOWN, TCS_SA_SCANONLY, 1, &result);
-       if(rtn == 0)
-       {
-               MSG_DEBUG("Detected malware number: %d\n", result.iNumDetected);
-               i = result.iNumDetected;
-               pDetected = result.pDList;
-               while(i && pDetected)
-               {
-                       int temp_b_level;
-                       int temp_s_class;
-                       MSG_SEC_DEBUG(" +-- Malware [%d] Name: %s\n", i, pDetected->pszName);
-                       MSG_DEBUG(" +-- Malware [%d] uAction: %u : 0x%04x\n", i, pDetected->uAction, pDetected->uAction);
-
-                       temp_b_level  = (pDetected->uAction & 0xFF00) >> 8;
-                       MSG_DEBUG(" +-- Malware [%d] Behavior level: %u\n", i, temp_b_level);
-
-                       if (ret_b_level == -1 || ret_b_level < temp_b_level) {
-                               ret_b_level = temp_b_level;
-                       }
+       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;
+       }
 
-                       temp_s_class  = (pDetected->uAction & 0x00FF);
-                       MSG_DEBUG(" +-- Malware [%d] Severity class: %u\n", i, temp_s_class);
+       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;
+               }
 
-                       pDetected = pDetected->pNext;
-                       i --;
+               if (NULL == detected) {
+                       MSG_DEBUG("Nothing detected");
+                       break;
                }
 
-               result.pfFreeResult(&result);
-       } else {
-               MSG_DEBUG("TCSScanFile fail: err = %d\n", rtn);
-       }
+               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);
 
-       TCSLibraryClose(hLib);
+               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);
 
-       if (bLevel)
-               *bLevel = ret_b_level;
+               if (name)
+                       free(name);
 
-       MSG_END();
+               if (bLevel)
+                       *bLevel = severity;
 
+       } while (0);
+
+       ret = _csr_cs_context_destroy(csr_handle);
+       if (ret != 0)
+               MSG_DEBUG("csr_cs_context_destroy error: err = %d\n", ret);
+
+       if (lib_handle)
+               dlclose(lib_handle);
+
+       MSG_END();
        return 0;
 }
+
+