da_bool_t is_valid_download_id(int dl_id) { da_bool_t ret = DA_TRUE; int slot_id = DA_INVALID_ID; DA_LOG_VERBOSE(Default, "[is_valid_download_id]download_id : %d", dl_id); ret = get_slot_id_for_dl_id(dl_id, &slot_id); if (ret != DA_RESULT_OK) { DA_LOG_ERR(Default, "dl req ID is not Valid"); ret = DA_FALSE; goto ERR; } else { ret = DA_TRUE; } if (DA_FALSE == is_valid_slot_id(slot_id)) { DA_LOG_ERR(Default, "Download ID is not Valid"); ret = DA_FALSE; goto ERR; } if (GET_DL_THREAD_ID(slot_id) < 1) { DA_LOG_ERR(Default, "Download thread is not alive"); ret = DA_FALSE; goto ERR; } ERR: return ret; }
da_result_t requesting_download(stage_info *stage) { da_result_t ret = DA_RESULT_OK; req_dl_info *request_session = DA_NULL; DA_LOG_FUNC_START(Default); if (!stage) { DA_LOG_ERR(Default, "stage is null.."); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } ret = make_req_dl_info_http(stage, GET_STAGE_TRANSACTION_INFO(stage)); if (ret != DA_RESULT_OK) goto ERR; request_session = GET_STAGE_TRANSACTION_INFO(stage); ret = request_http_download(stage); if (DA_RESULT_OK == ret) { DA_LOG(Default, "Http download is complete."); } else { DA_LOG_ERR(Default, "Http download is failed. ret = %d", ret); goto ERR; } ERR: return ret; }
da_result_t cancel_download(int dl_id) { da_result_t ret = DA_RESULT_OK; int slot_id = DA_INVALID_ID; DA_LOG_FUNC_START(Default); ret = get_slot_id_for_dl_id(dl_id, &slot_id); if (ret != DA_RESULT_OK) { DA_LOG_ERR(Default, "dl req ID is not Valid"); goto ERR; } if (DA_FALSE == is_valid_slot_id(slot_id)) { DA_LOG_ERR(Default, "Download ID is not Valid"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } ret = __cancel_download_with_slot_id(slot_id); ERR: return ret; }
da_result_t suspend_download(int dl_id, da_bool_t is_enable_cb) { da_result_t ret = DA_RESULT_OK; int slot_id = DA_INVALID_ID; DA_LOG_FUNC_START(Default); ret = get_slot_id_for_dl_id(dl_id, &slot_id); if (ret != DA_RESULT_OK) { DA_LOG_ERR(Default, "dl req ID is not Valid"); goto ERR; } GET_DL_ENABLE_PAUSE_UPDATE(slot_id) = is_enable_cb; if (DA_FALSE == is_valid_slot_id(slot_id)) { DA_LOG_ERR(Default, "Download ID is not Valid"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } ret = __suspend_download_with_slot_id(slot_id); ERR: return ret; }
da_result_t send_client_update_dl_info ( int download_id, int dl_req_id, char *file_type, unsigned long int file_size, char *tmp_saved_path, char *http_response_header, char *http_chunked_data ) { client_noti_t *client_noti = DA_NULL; user_download_info_t *update_dl_info = DA_NULL; DA_LOG_FUNC_START(ClientNoti); if (!is_valid_dl_ID(download_id)) { DA_LOG_ERR(ClientNoti, "Download ID is not valid"); return DA_ERR_INVALID_DL_REQ_ID; } client_noti = (client_noti_t *)calloc(1, sizeof(client_noti_t)); if (!client_noti) { DA_LOG_ERR(ClientNoti, "calloc fail"); return DA_ERR_FAIL_TO_MEMALLOC; } client_noti->download_id = download_id; client_noti->user_data = GET_DL_USER_DATA(download_id); client_noti->noti_type = Q_CLIENT_NOTI_TYPE_UPDATE_DL_INFO; client_noti->next = DA_NULL; update_dl_info = (user_download_info_t *)&(client_noti->type.update_dl_info); update_dl_info->da_dl_req_id = dl_req_id; update_dl_info->file_size = file_size; /* These strings MUST be copied to detach __thread_for_client_noti from download_info */ if (file_type) update_dl_info->file_type = strdup(file_type); if (tmp_saved_path) update_dl_info->tmp_saved_path = strdup(tmp_saved_path); if (http_response_header) { update_dl_info->http_response_header = strdup(http_response_header); } if (http_chunked_data) { update_dl_info->http_chunked_data = calloc (1, file_size); if (update_dl_info->http_chunked_data) memcpy(update_dl_info->http_chunked_data, http_chunked_data, file_size); } DA_LOG(ClientNoti, "pushing file_size=%lu, download_id=%d, dl_req_id=%d", file_size, download_id, dl_req_id); push_client_noti(client_noti); return DA_RESULT_OK; }
da_result_t send_client_da_state (int download_id, da_state state, int err) { client_noti_t *client_noti = DA_NULL; user_notify_info_t *send_state_info = DA_NULL; da_state cur_da_state; DA_LOG_FUNC_START(ClientNoti); DA_LOG_VERBOSE(ClientNoti, "da_state[%s], download_id[%d]", print_dl_state(state), download_id); if (!is_valid_dl_ID(download_id)) { DA_LOG_ERR(ClientNoti, "Download ID is not valid"); /* Returning DA_RESULT_OK if download_id is not valid, * because sending da_state should not effect to download flow. */ return DA_RESULT_OK; } if (state_watcher_need_redirect_Q(download_id)) { state_watcher_redirect_state(download_id, state, err); return DA_RESULT_OK; } cur_da_state = GET_DL_DA_STATE(download_id); if ((DA_STATE_SUSPENDED != state) && (cur_da_state == state)) { DA_LOG(ClientNoti, "inserting da_state is same with current da_state! Not inserting! inserting: %d, cur : %d", state, cur_da_state); return DA_RESULT_OK; } GET_DL_DA_STATE(download_id) = state; DA_LOG_VERBOSE(ClientNoti, "change da_state to %d", state); client_noti = (client_noti_t *)calloc(1, sizeof(client_noti_t)); if (!client_noti) { DA_LOG_ERR(ClientNoti, "calloc fail"); return DA_ERR_FAIL_TO_MEMALLOC; } client_noti->download_id = download_id; client_noti->user_data = GET_DL_USER_DATA(download_id); client_noti->noti_type = Q_CLIENT_NOTI_TYPE_SEND_STATE; client_noti->next = DA_NULL; send_state_info = (user_notify_info_t *)&(client_noti->type.da_state_info); send_state_info->da_dl_req_id = GET_DL_REQ_ID(download_id); send_state_info->state = state; send_state_info->err = err; DA_LOG(ClientNoti, "pushing da_state=%d, download_id=%d, err=%d, dl_req_id=%d", state, download_id, err, GET_DL_REQ_ID(download_id)); push_client_noti(client_noti); return DA_RESULT_OK; }
int read_data_from_file(char *file, char **out_buffer) { FILE *fd; int file_size = -1; char *buffer = NULL; int buffer_len = 0; int read_len = 0; *out_buffer = NULL; if (!file) return 0; /* open file with "rb", because fread() handles the file as binary mode */ fd = fopen(file, "rb"); if (!fd) { DA_LOG_ERR(FileManager,"File open err! received file path = [%s]", file); return 0; } get_file_size(file, &file_size); if (file_size <= 0) { DA_LOG_ERR(FileManager,"file size is [%d]", file_size); fclose(fd); return 0; } /* A guide from www.securecoding.cert.org * : FIO17-C. Do not rely on an ending null character when using fread() * * buffer is initialized with null through calloc(), so, it is always null-terminated even if fread() failed. * allocate memory one more byte to ensure null-terminated even if the file is not null-terminated. */ buffer_len = sizeof(char) * file_size; buffer = (char *)calloc(1, buffer_len + 1); if (buffer) { read_len = fread(buffer, sizeof(char), file_size, fd); if (read_len == file_size) { *out_buffer = buffer; } else { DA_LOG_ERR(FileManager,"File Read Not Complete read length = %d", read_len); free(buffer); buffer = NULL; buffer_len = 0; } } else { buffer_len = 0; } fclose(fd); return buffer_len; }
da_result_t check_enough_storage(stage_info *stage) { da_result_t ret = DA_RESULT_OK; unsigned long cont_size = 0; da_storage_size_t avail_memory = {0, }; da_storage_type_t type = DA_STORAGE_PHONE; DA_LOG_FUNC_START(Default); /* check enough storage here because of multiple download */ ret = get_storage_type(&type); if (DA_RESULT_OK != ret) return ret; cont_size = GET_CONTENT_STORE_CURRENT_FILE_SIZE(GET_STAGE_CONTENT_STORE_INFO(stage)); DA_LOG(Default,"cont_size [%d]", cont_size); if (cont_size) { ret = get_available_memory(DA_STORAGE_PHONE, &avail_memory); if (DA_RESULT_OK == ret && (avail_memory.b_available < ((cont_size + SAVE_FILE_BUFFERING_SIZE_5MB) / avail_memory.b_size))) /* 5MB buffering */ { DA_LOG_ERR(Default,"There is no space for content in Phone memory"); return DA_ERR_DISK_FULL; } else if (DA_RESULT_OK != ret) { return ret; } /* If default memeory type is mmc, * it need to check phone memroy for temproary folder * and mmc memory for actual install folder */ if (type == DA_STORAGE_MMC) { DA_LOG(Default,"Storage type == DA_STORAGE_MMC\n"); ret = get_available_memory(type, &avail_memory); if (DA_RESULT_OK == ret && (avail_memory.b_available < ((cont_size + SAVE_FILE_BUFFERING_SIZE_50KB) / avail_memory.b_size))) /* 50KB buffering */ { DA_LOG_ERR(Default,"There is no space for content in MMC memory"); return DA_ERR_DISK_FULL; } else if (DA_RESULT_OK != ret) { return ret; } } } return ret; }
da_bool_t is_valid_url(const char* url, da_result_t *err_code) { da_result_t ret = DA_RESULT_OK; da_bool_t b_ret = DA_FALSE; int wanted_str_len = 0; char *wanted_str = NULL; char *wanted_str_start = NULL; char *wanted_str_end = NULL; if ((DA_NULL == url) || (1 > strlen(url))) { ret = DA_ERR_INVALID_URL; goto ERR; } wanted_str_start = (char*)url; wanted_str_end = strstr(url, "://"); if (!wanted_str_end) { DA_LOG_ERR(Default,"No protocol on this url"); ret = DA_ERR_INVALID_URL; goto ERR; } wanted_str_len = wanted_str_end - wanted_str_start; wanted_str = (char*)calloc(1, wanted_str_len + 1); if (!wanted_str) { DA_LOG_ERR(Default,"DA_ERR_FAIL_TO_MEMALLOC"); ret = DA_ERR_FAIL_TO_MEMALLOC; goto ERR; } strncpy(wanted_str, wanted_str_start, wanted_str_len); b_ret = is_supporting_protocol(wanted_str); if (!b_ret) { ret = DA_ERR_UNSUPPORTED_PROTOCAL; goto ERR; } ERR: if (wanted_str) { free(wanted_str); wanted_str = NULL; } if (err_code) *err_code = ret; return b_ret; }
da_bool_t is_base64_encoded_word(const char *in_str) { const char *haystack = DA_NULL; char first_needle[8] = {0,}; char second_needle[8] = {0,}; char *found_str = DA_NULL; if (!in_str) { DA_LOG_ERR(Default, "input string is NULL"); return DA_FALSE; } haystack = in_str; if (haystack[0] == '"') { snprintf(first_needle, sizeof(first_needle), "%s", "\"=?"); // "=? snprintf(second_needle, sizeof(second_needle), "%s", "?=\""); // ?=" } else { snprintf(first_needle, sizeof(first_needle), "%s", "=?"); // =? snprintf(second_needle, sizeof(second_needle), "%s", "?="); // ?= } // DA_LOG(Default, "needle = [%s], haystack = [%s]", first_needle, haystack); found_str = strstr(haystack, first_needle); if (found_str) { if (found_str == haystack) { // DA_LOG(Default, "Input string is starting with %s", needle); haystack = haystack + strlen(haystack) - strlen(second_needle); // DA_LOG(Default, "second haystack is [%s]", haystack); if(!strcmp(haystack, second_needle)) return DA_TRUE; } } return DA_FALSE; }
da_result_t dereg_client_app(void) { client_noti_t *client_noti = DA_NULL; DA_LOG_FUNC_START(ClientNoti); client_noti = (client_noti_t *)calloc(1, sizeof(client_noti_t)); if (!client_noti) { DA_LOG_ERR(ClientNoti, "calloc fail"); return DA_ERR_FAIL_TO_MEMALLOC; } client_noti->download_id = DA_INVALID_ID; client_noti->noti_type = Q_CLIENT_NOTI_TYPE_TERMINATE; client_noti->next = DA_NULL; _da_thread_mutex_lock(&(client_app_mgr.mutex_client_mgr)); if (client_app_mgr.is_thread_init != DA_TRUE) { DA_LOG_CRITICAL(ClientNoti, "try to cancel client mgr thread id[%lu]", client_app_mgr.thread_id); if (pthread_cancel(client_app_mgr.thread_id) < 0) { DA_LOG_ERR(ClientNoti, "cancel thread is failed!!!"); } } else { void *t_return = NULL; DA_LOG_VERBOSE(ClientNoti, "pushing Q_CLIENT_NOTI_TYPE_TERMINATE"); push_client_noti(client_noti); DA_LOG_CRITICAL(Thread, "===try to join client mgr thread id[%lu]===", client_app_mgr.thread_id); if (pthread_join(client_app_mgr.thread_id, &t_return) < 0) { DA_LOG_ERR(Thread, "join client thread is failed!!!"); } DA_LOG_CRITICAL(Thread, "===thread join return[%d]===", (char*)t_return); } _da_thread_mutex_unlock(&(client_app_mgr.mutex_client_mgr)); /* ToDo: This clean up should be done at the end of client_thread. */ client_app_mgr.client_app_info.is_using= DA_FALSE; client_app_mgr.client_app_info.is_manual_download = DA_FALSE; if(client_app_mgr.client_app_info.client_user_agent) { free(client_app_mgr.client_app_info.client_user_agent); client_app_mgr.client_app_info.client_user_agent = DA_NULL; } _da_thread_mutex_lock(&(client_app_mgr.mutex_client_mgr)); client_app_mgr.is_thread_init = DA_FALSE; _da_thread_mutex_unlock(&(client_app_mgr.mutex_client_mgr)); _da_thread_mutex_destroy(&(client_app_mgr.mutex_client_mgr)); return DA_RESULT_OK; }
da_bool_t da_get_extension_name_from_url(char *url, char **ext) { da_bool_t ret = DA_TRUE; char *buff = DA_NULL; char *temp_str = DA_NULL; int buf_len = 0; DA_LOG_FUNC_START(Default); if (DA_NULL == url || DA_NULL == ext) { ret = DA_FALSE; DA_LOG_ERR(Default,"Invalid Argument"); return ret; } if ((temp_str = strrchr(url,'/'))) { if ((buff = strrchr(temp_str,'.'))) { char *q = DA_NULL; buff++; /* check to exist "?" after extension name */ q = strrchr(buff,'?'); if (q) { buf_len = strlen(buff) - strlen(q); } else { buf_len = strlen(buff); } *ext = (char*) calloc(1, buf_len + 1) ; if (DA_NULL == *ext) { ret = DA_FALSE; DA_LOG_ERR(Default,"Memory Fail"); goto ERR; } strncpy(*ext,buff,buf_len); DA_LOG(Default,"extention name[%s]",*ext); return ret; } } ERR: if (*ext) { free(*ext); *ext = DA_NULL; } return ret; }
void push_client_noti(client_noti_t *client_noti) { client_queue_t *queue = DA_NULL; client_noti_t *head = DA_NULL; client_noti_t *pre = DA_NULL; client_noti_t *cur = DA_NULL; /* DA_LOG_FUNC_START(ClientNoti); */ if(!is_this_client_available()) { DA_LOG_ERR(ClientNoti, "invalid client"); return; } queue = &(client_app_mgr.client_queue); _da_thread_mutex_lock (&(queue->mutex_client_queue)); head = queue->client_q_head; if (!head) { queue->client_q_head = client_noti; } else { cur = head; while (cur->next) { pre = cur; cur = pre->next; } if (DA_TRUE == is_this_client_manual_download_type()) { cur->next = client_noti; } else if (client_noti->noti_type == Q_CLIENT_NOTI_TYPE_SEND_STATE) { cur->next = client_noti; } else { if (cur->noti_type == Q_CLIENT_NOTI_TYPE_SEND_STATE) { cur->next = client_noti; } else if (cur->noti_type == Q_CLIENT_NOTI_TYPE_UPDATE_DOWNLOADING_INFO) { /* For UI performance. If the update noti info is existed at queue, replace it with new update noti info */ if (cur->download_id == client_noti->download_id) { /* DA_LOG(ClientNoti, "exchange queue's tail and pushing item"); */ if (pre == DA_NULL) queue->client_q_head = client_noti; else pre->next = client_noti; destroy_client_noti(cur); } else { cur->next = client_noti; } } } } queue->having_data = DA_TRUE; __client_q_wake_up_without_lock(); _da_thread_mutex_unlock (&(queue->mutex_client_queue)); }
da_result_t get_available_memory( da_storage_type_t storage_type, da_storage_size_t *avail_memory) { int fs_ret = 0; struct statfs filesys_info = {0, }; DA_LOG_FUNC_START(Default); if (!avail_memory) return DA_ERR_INVALID_ARGUMENT; if (storage_type == DA_STORAGE_PHONE) { fs_ret = statfs(DA_DEFAULT_TMP_FILE_DIR_PATH, &filesys_info); } else if (storage_type == DA_STORAGE_MMC) { char *default_install_dir = NULL; default_install_dir = PI_get_default_install_dir(); if (default_install_dir) { fs_ret = statfs(default_install_dir, &filesys_info); } else { return DA_ERR_FAIL_TO_ACCESS_STORAGE; } } else { DA_LOG_ERR(Default,"Invalid storage type"); return DA_ERR_INVALID_ARGUMENT; } if (fs_ret != 0) { DA_LOG_ERR(Default,"Phone file path :statfs error - [%d]", errno); return DA_ERR_FAIL_TO_ACCESS_FILE; } avail_memory->b_available = filesys_info.f_bavail; avail_memory->b_size = filesys_info.f_bsize; DA_LOG(Default, "Memory type : %d", storage_type); DA_LOG(Default, "Available Memory(f_bavail) : %lu", filesys_info.f_bavail); DA_LOG(Default, "Available Memory(f_bsize) : %d", filesys_info.f_bsize); DA_LOG(Default, "Available Memory(kbytes) : %lu", (filesys_info.f_bavail/1024)*filesys_info.f_bsize); return DA_RESULT_OK; }
da_result_t send_client_update_downloading_info ( int download_id, int dl_req_id, unsigned long int total_received_size, char *saved_path ) { client_noti_t *client_noti = DA_NULL; user_downloading_info_t *downloading_info = DA_NULL; DA_LOG_FUNC_START(ClientNoti); if (!is_valid_dl_ID(download_id)) { DA_LOG_ERR(ClientNoti, "Download ID is not valid"); return DA_ERR_INVALID_DL_REQ_ID; } client_noti = (client_noti_t *)calloc(1, sizeof(client_noti_t)); if (!client_noti) { DA_LOG_ERR(ClientNoti, "calloc fail"); return DA_ERR_FAIL_TO_MEMALLOC; } client_noti->download_id = download_id; client_noti->user_data = GET_DL_USER_DATA(download_id); client_noti->noti_type = Q_CLIENT_NOTI_TYPE_UPDATE_DOWNLOADING_INFO; client_noti->next = DA_NULL; downloading_info = (user_downloading_info_t *)&(client_noti->type.update_downloading_info); downloading_info->da_dl_req_id = dl_req_id; downloading_info->total_received_size = total_received_size; /* These strings MUST be copied to detach __thread_for_client_noti from download_info */ if (saved_path) downloading_info->saved_path = strdup(saved_path); DA_LOG(ClientNoti, "pushing received_size=%lu, download_id=%d, dl_req_id=%d", total_received_size, download_id, dl_req_id); push_client_noti(client_noti); return DA_RESULT_OK; }
void remove_file(const char *file_path) { DA_LOG_FUNC_START(FileManager); if (file_path) { DA_LOG(FileManager,"remove file [%s]", file_path); if (unlink(file_path) < 0) { DA_LOG_ERR(FileManager,"file removing failed."); } } }
da_result_t get_client_download_path(char **out_path) { if (!out_path) { DA_LOG_ERR(ClientNoti, "DA_ERR_INVALID_ARGUMENT"); return DA_ERR_INVALID_ARGUMENT; } /* change the directory policy. it doesn't clean the temp direcoty when deinitializing */ *out_path = strdup(DA_DEFAULT_TMP_FILE_DIR_PATH); DA_LOG_VERBOSE(ClientNoti, "client download path = [%s]", *out_path); return DA_RESULT_OK; }
da_result_t get_extension_from_mime_type(char *mime_type, char **extension) { da_result_t ret = DA_RESULT_OK; char *ext = DA_NULL; DA_LOG_FUNC_START(Default); if (DA_NULL == mime_type || DA_NULL == extension) { DA_LOG_ERR(Default,"received mime_type is null"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } DA_LOG(Default,"input mime type = %s", mime_type); if (DA_RESULT_OK != (ret = da_mime_get_ext_name(mime_type, &ext))) { DA_LOG_ERR(Default,"can't find proper extension!"); goto ERR; } *extension = ext; DA_LOG(Default,"found extension = %s", *extension); ERR: return ret; }
da_result_t __launch_client_thread(void) { pthread_t thread_id = DA_NULL; DA_LOG_FUNC_START(Thread); if (pthread_create(&thread_id,DA_NULL,__thread_for_client_noti,DA_NULL) < 0) { DA_LOG_ERR(Thread, "making thread failed.."); return DA_ERR_FAIL_TO_CREATE_THREAD; } DA_LOG(Thread, "client mgr thread id[%d]", thread_id); client_app_mgr.thread_id = thread_id; return DA_RESULT_OK; }
void delete_prohibited_char(char *szTarget, int str_len) { char *chk_str = NULL; int i = 0; int j = 0; int tar_len = 0; if(szTarget == NULL || str_len <= 0 || strlen(szTarget) != str_len) { DA_LOG_ERR(Default,"Invaild Parameter\n"); return; } chk_str = (char *)calloc(1, str_len + 1); if(chk_str == NULL) return; while(szTarget[j] != '\0') { if(IS_PROHIBITED_CHAR(szTarget[j]) == DA_FALSE && IS_SPACE_CHARACTER(szTarget[j]) == DA_FALSE) { chk_str[i] = szTarget[j]; i++; } j++; } chk_str[i] = '\0'; tar_len = strlen(chk_str); if(tar_len <= 0) szTarget[0] = '\0'; else { for(i = 0; i < tar_len; i++) { szTarget[i] = chk_str[i]; } szTarget[i] = '\0'; } if(chk_str != NULL) { free(chk_str); } return; }
static void *__thread_for_client_noti(void *data) { da_result_t ret = DA_RESULT_OK; da_bool_t need_wait = DA_TRUE; client_queue_t *queue = DA_NULL; client_noti_t *client_noti = DA_NULL; DA_LOG_FUNC_START(Thread); _da_thread_mutex_lock(&(client_app_mgr.mutex_client_mgr)); client_app_mgr.is_thread_init = DA_TRUE; _da_thread_mutex_unlock(&(client_app_mgr.mutex_client_mgr)); queue = &(client_app_mgr.client_queue); DA_LOG(ClientNoti, "client queue = %p", queue); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, DA_NULL); pthread_cleanup_push(__thread_clean_up_handler_for_client_thread, (void *)DA_NULL); do { _da_thread_mutex_lock(&(queue->mutex_client_queue)); if (DA_FALSE == IS_CLIENT_Q_HAVING_DATA(queue)) { DA_LOG(Thread, "Sleep @ thread_for_client_noti!"); __client_q_goto_sleep_without_lock(); DA_LOG(Thread, "Woke up @ thread_for_client_noti"); } _da_thread_mutex_unlock(&(queue->mutex_client_queue)); do { __pop_client_noti(&client_noti); if (client_noti == DA_NULL) { DA_LOG_ERR(ClientNoti, "There is no data on client queue!"); ret = DA_ERR_INVALID_STATE; need_wait = DA_FALSE; } else { switch (client_noti->noti_type) { case Q_CLIENT_NOTI_TYPE_UPDATE_DL_INFO: { user_download_info_t *update_dl_info = DA_NULL;; update_dl_info = (user_download_info_t*)(&(client_noti->type.update_dl_info)); if (client_app_mgr.client_app_info.client_callback.update_dl_info_cb) { client_app_mgr.client_app_info.client_callback.update_dl_info_cb(update_dl_info, client_noti->user_data); DA_LOG(ClientNoti, "Update download info for download_id=%d, dl_req_id=%d, received size=%lu- DONE", client_noti->download_id, update_dl_info->da_dl_req_id, update_dl_info->file_size ); } } break; case Q_CLIENT_NOTI_TYPE_UPDATE_DOWNLOADING_INFO: { user_downloading_info_t *downloading_info = DA_NULL;; downloading_info = (user_downloading_info_t*)(&(client_noti->type.update_downloading_info)); if (client_app_mgr.client_app_info.client_callback.update_progress_info_cb) { client_app_mgr.client_app_info.client_callback.update_progress_info_cb(downloading_info, client_noti->user_data); DA_LOG(ClientNoti, "Update downloading info for download_id=%d, dl_req_id=%d, received size=%lu - DONE", client_noti->download_id, downloading_info->da_dl_req_id, downloading_info->total_received_size); } } break; case Q_CLIENT_NOTI_TYPE_SEND_STATE: { user_notify_info_t *da_state_info = DA_NULL; da_state_info = (user_notify_info_t *)(&(client_noti->type.da_state_info)); if (client_app_mgr.client_app_info.client_callback.user_noti_cb) { DA_LOG(ClientNoti, "User Noti info for download_id=%d, dl_req_id=%d, da_state=%d, err=%d", client_noti->download_id, da_state_info->da_dl_req_id, da_state_info->state, da_state_info->err); client_app_mgr.client_app_info.client_callback.user_noti_cb(da_state_info, client_noti->user_data); DA_LOG(ClientNoti, "User Noti info for download_id=%d, dl_req_id=%d, da_state=%d, err=%d - DONE", client_noti->download_id, da_state_info->da_dl_req_id, da_state_info->state, da_state_info->err); } } break; case Q_CLIENT_NOTI_TYPE_TERMINATE: DA_LOG_CRITICAL(ClientNoti, "Q_CLIENT_NOTI_TYPE_TERMINATE"); need_wait = DA_FALSE; break; } destroy_client_noti(client_noti); } if(DA_TRUE == need_wait) { _da_thread_mutex_lock(&(queue->mutex_client_queue)); if (DA_FALSE == IS_CLIENT_Q_HAVING_DATA(queue)) { _da_thread_mutex_unlock (&(queue->mutex_client_queue)); break; } else { _da_thread_mutex_unlock (&(queue->mutex_client_queue)); } } else { break; } } while (1); } while (DA_TRUE == need_wait); _da_thread_mutex_destroy(&(queue->mutex_client_queue)); _da_thread_cond_destroy(&(queue->cond_client_queue)); pthread_cleanup_pop(0); DA_LOG_CRITICAL(Thread, "=====thread_for_client_noti- EXIT====="); pthread_exit((void *)NULL); return DA_NULL; }
da_result_t da_mime_get_ext_name(char *mime, char **ext) { da_result_t ret = DA_RESULT_OK; const char **extlist = DA_NULL; const char *unaliased_mimetype = DA_NULL; char ext_temp[DA_MAX_STR_LEN] = {0,}; char *temp = NULL; DA_LOG_FUNC_START(Default); if (DA_NULL == mime || DA_NULL == ext) { ret = DA_ERR_INVALID_ARGUMENT; DA_LOG_ERR(Default,"Invalid mime type"); goto ERR; } DA_LOG_VERBOSE(Default,"mime str[%s]ptr[%p]len[%d]",mime,mime,strlen(mime)); /* unaliased_mimetype means representative mime among similar types */ unaliased_mimetype = xdg_mime_unalias_mime_type(mime); if (unaliased_mimetype == DA_NULL) { ret = DA_ERR_INVALID_MIME_TYPE; DA_LOG_ERR(Default,"Invalid mime type : No unsaliased mime type"); goto ERR; } DA_LOG(Default,"unaliased_mimetype[%s]\n",unaliased_mimetype); /* Get extension name from shared-mime-info */ extlist = xdg_mime_get_file_names_from_mime_type(unaliased_mimetype); if (extlist == DA_NULL || *extlist == DA_NULL) { int i = 0; ret = DA_ERR_INVALID_MIME_TYPE; DA_LOG(Default,"No extension list"); #ifdef _SAMSUNG_MIME_POLICY for (i = 0; i < MAX_SEC_MIME_TABLE_INDEX; i++) { if (strncmp(sec_mime_table[i].mime, mime, strlen(mime)) == 0) { strncpy(ext_temp, sec_mime_table[i].ext, DA_MAX_STR_LEN-1); ret = DA_RESULT_OK; break; } } #endif } else { /* For drm case, this else statement is needed */ DA_LOG(Default,"extlist[%s]\n",*extlist); strncpy(ext_temp, *extlist, DA_MAX_STR_LEN); /* If only one extension name is existed, don't enter here */ while (*extlist != NULL) { int i = 0; /* If there are existed many extension names, * try to search common extension name from table * with first mime type at extension list*/ for (i = 0; i < MAX_EXT_TABLE_INDEX; i++) { if (strncmp(ext_trans_table[i].standard,*extlist, strlen(*extlist)) == 0) { memset(ext_temp, 0x00, DA_MAX_STR_LEN); strncpy(ext_temp,ext_trans_table[i].normal, DA_MAX_STR_LEN-1); break; } } DA_LOG(Default,"index[%d]\n",i); /* If there is a mime at extension transform table */ if (i < MAX_EXT_TABLE_INDEX) { break; } DA_LOG(Default,"extlist[%s]\n",*extlist); extlist++; } DA_LOG(Default,"extension from shared mime info[%s]",ext_temp); } if (strlen(ext_temp) < 1) { /* If there is no mime string for OMD descriptor mime type */ if (strncmp(DD_MIME_STR,mime,strlen(DD_MIME_STR)) == 0) { strncpy(ext_temp, DD_EXT_STR, DA_MAX_STR_LEN-1); ret = DA_RESULT_OK; /* If there is no extension name for "applicaion/vnd.oma.drm.messeages" * at shared-mime-info*/ } else if (strncmp(DRM_MIME_STR,mime,strlen(DD_MIME_STR)) == 0) { strncpy(ext_temp, DRM_EXT_STR, DA_MAX_STR_LEN-1); /* If there is extension name at extlist, the return value can have an error.*/ ret = DA_RESULT_OK; } else { ret = DA_ERR_INVALID_MIME_TYPE; DA_LOG_ERR(Default,"Invalid mime type : no extension name at list"); } } if (ret != DA_RESULT_OK) goto ERR; temp = strchr(ext_temp,'.'); if (temp == NULL) temp = ext_temp; else temp++; DA_LOG(Default,"final extension name:[%s]",temp); *ext = (char*)calloc(1, strlen(temp) + 1); if (*ext != DA_NULL) { strncpy(*ext, temp,strlen(temp)); } else { ret = DA_ERR_FAIL_TO_MEMALLOC ; goto ERR ; } ERR: return ret; }
da_result_t _parsing_base64_encoded_str(const char *in_encoded_str, char **out_charset_type, char *out_encoding_type, char **out_raw_encoded_str) { da_result_t ret = DA_RESULT_OK; const char *org_str = DA_NULL; // e.g. =?UTF-8?B?7Jew7JWE7JmA7IKs7J6QLmpwZw==?= char *charset_type = NULL; // e.g. UTF-8 char encoding_type = '\0'; // e.g. B (means Base64) char *raw_encoded_str = NULL; // e.g. 7Jew7JWE7JmA7IKs7J6QLmpwZw== char *haystack = DA_NULL; char needle[8] = {0,}; char *wanted_str = DA_NULL; int wanted_str_len = 0; char *wanted_str_start = DA_NULL; char *wanted_str_end = DA_NULL; org_str = in_encoded_str; if (!org_str) { DA_LOG_ERR(Default, "Input string is NULL"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } // strip "=?" haystack = (char*)org_str; snprintf(needle, sizeof(needle), "=?"); wanted_str_end = strstr(haystack, needle); if (!wanted_str_end) { DA_LOG_ERR(Default, "DA_ERR_INVALID_ARGUMENT"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } else { wanted_str = wanted_str_end + strlen(needle); DA_LOG(Default, "strip [%s]", wanted_str); } // for charset haystack = wanted_str_start = wanted_str; needle[0] = '?'; wanted_str_end = strchr(haystack, needle[0]); if (!wanted_str_end) { DA_LOG_ERR(Default, "DA_ERR_INVALID_ARGUMENT"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } else { wanted_str_len = wanted_str_end - wanted_str_start + 1; wanted_str = (char*)calloc(1, wanted_str_len+1); if (!wanted_str) { DA_LOG_ERR(Default, "DA_ERR_FAIL_TO_MEMALLOC"); ret = DA_ERR_FAIL_TO_MEMALLOC; goto ERR; } else { snprintf(wanted_str, wanted_str_len+1, "%s", wanted_str_start); charset_type = wanted_str; wanted_str = DA_NULL; } DA_LOG(Default, "charset [%s]", charset_type); } // for encoding encoding_type = *(++wanted_str_end); DA_LOG(Default, "encoding [%c]", encoding_type); // for raw encoded str haystack = wanted_str_start = wanted_str_end + 1; snprintf(needle, sizeof(needle), "?="); wanted_str_end = strstr(haystack, needle); if (!wanted_str_end) { DA_LOG_ERR(Default, "DA_ERR_INVALID_ARGUMENT"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } else { wanted_str_len = wanted_str_end - wanted_str_start + 1; wanted_str = (char*)calloc(1, wanted_str_len+1); if (!wanted_str) { DA_LOG_ERR(Default, "DA_ERR_FAIL_TO_MEMALLOC"); ret = DA_ERR_FAIL_TO_MEMALLOC; goto ERR; } else { snprintf(wanted_str, wanted_str_len+1, "%s", wanted_str_start); raw_encoded_str = wanted_str; wanted_str = NULL; } DA_LOG(Default, "raw encoded str [%s]", raw_encoded_str); } ERR: if (ret != DA_RESULT_OK) { if (charset_type) { free(charset_type); charset_type = NULL; } } *out_charset_type = charset_type; *out_encoding_type = encoding_type; *out_raw_encoded_str = raw_encoded_str; return ret; }
da_result_t decode_base64_encoded_str(const char *in_encoded_str, char **out_decoded_ascii_str) { da_result_t ret = DA_RESULT_OK; const char *org_str = DA_NULL; char *charset_type = NULL; char encoding_type = '\0'; char *raw_encoded_str = NULL; char *decoded_str = NULL; const gchar *g_encoded_text = NULL; guchar *g_decoded_text = NULL; gsize g_decoded_text_len = 0; DA_LOG(Default, "input str = [%s]", in_encoded_str); org_str = in_encoded_str; if(!org_str) { DA_LOG_ERR(Default, "Input string is NULL"); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } ret = _parsing_base64_encoded_str(org_str, &charset_type, &encoding_type, &raw_encoded_str); if(ret != DA_RESULT_OK) { goto ERR; } // DA_LOG(Default, "charset = [%s], encoding = [%c], raw = [%s]", charset_type, encoding_type, raw_encoded_str); if(encoding_type != 'B') { DA_LOG_ERR(Default, "Encoded Word is not encoded with Base64, but %c. We can only handle Base64.", encoding_type); ret = DA_ERR_INVALID_ARGUMENT; goto ERR; } /* * on glib/gtype.h * typedef char gchar; * typedef unsigned char guchar; * */ g_encoded_text = (const gchar*)raw_encoded_str; g_decoded_text = g_base64_decode(g_encoded_text, &g_decoded_text_len); if(g_decoded_text) { DA_LOG(Default, "g_decoded_text = [%s]", g_decoded_text); decoded_str = (char*)calloc(1, g_decoded_text_len+1); if(!decoded_str) { DA_LOG_ERR(Default, "DA_ERR_FAIL_TO_MEMALLOC"); ret = DA_ERR_FAIL_TO_MEMALLOC; goto ERR; } else { memcpy(decoded_str, g_decoded_text, g_decoded_text_len); } } DA_LOG(Default, "decoded_str = [%s]", decoded_str); ERR: *out_decoded_ascii_str = decoded_str; if(charset_type) { free(charset_type); charset_type = NULL; } if(raw_encoded_str) { free(raw_encoded_str); raw_encoded_str = NULL; } if(g_decoded_text) { g_free(g_decoded_text); } return ret; }
/* FIXME move this function to another file */ da_bool_t da_get_file_name_from_url(char* url, char** name) { da_bool_t ret = DA_TRUE; char *buff = DA_NULL; char *Start = NULL; char *End = NULL; char c = 0; int i = 0; int j = 0; int len_name = 0; char name_buff[DA_MAX_FILE_PATH_LEN] = {0,}; DA_LOG_FUNC_START(Default); if (DA_NULL == url || DA_NULL == name) { ret = DA_FALSE; DA_LOG_ERR(Default,"Invalid Argument"); goto ERR; } *name = DA_NULL; if (!strstr(url, "http") && !strstr(url, "https")) { ret = DA_FALSE; DA_LOG_ERR(Default,"Invalid Argument"); goto ERR; } buff = (char*) calloc(1, strlen(url) +1); if(DA_NULL == buff) { ret = DA_FALSE; DA_LOG_ERR(Default,"Memory Fail"); goto ERR; } while((c = url[i++]) != 0) { if(c == '%') { char buffer[3] = {0,}; buffer[0] = url[i++]; buffer[1] = url[i++]; buff[j++] = (char)strtol(buffer,NULL,16); } else { buff[j++] = c; } } End = strstr(buff, "?"); if (DA_NULL != End) { Start = End -1; while(*(Start) != '/') { Start--; } if ((*(Start) == '/') && ((len_name = (End - Start)) > 1)) { Start++; if (DA_MAX_FILE_PATH_LEN <= len_name) { strncpy(name_buff, Start, DA_MAX_FILE_PATH_LEN-1); name_buff[DA_MAX_FILE_PATH_LEN-1] = '\0'; } else { strncpy(name_buff, Start, len_name); name_buff[len_name] = '\0'; } } else { ret = DA_FALSE; goto ERR ; /*Name not found*/ } } else { int urlLen = strlen (buff); int Start_pos = 0; Start_pos = urlLen - 1; while(Start_pos > 0) { if(buff[Start_pos] == '/') break; Start_pos--; } Start_pos++; if (Start_pos == 0 || urlLen - Start_pos <= 0) { ret = DA_FALSE; goto ERR; } while(Start_pos < urlLen) { name_buff[len_name++] = buff[Start_pos++]; if (DA_MAX_FILE_PATH_LEN <= len_name) { name_buff[DA_MAX_FILE_PATH_LEN-1] ='\0'; break; } } } if (len_name) { End = strrchr(name_buff, '.'); if (End != NULL) { *End = '\0'; } DA_LOG(Default,"file name BEFORE removing prohibited character = %s", name_buff); delete_prohibited_char(name_buff, strlen(name_buff)); DA_LOG(Default,"file name AFTER removing prohibited character = %s", name_buff); len_name = strlen(name_buff); *name = (char*) calloc(1, len_name + 1); if (*name) { strncpy(*name, name_buff,len_name); } } DA_LOG(Default,"Extracted file name : %s", *name); ERR: if (buff) { free (buff); buff = DA_NULL; } return ret; }