2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <curl/curl.h>
22 #include "download-agent-debug.h"
23 #include "download-agent-encoding.h"
25 da_ret_t _parsing_base64_encoded_str(const char *in_encoded_str,
26 char **out_charset_type,
27 char *out_encoding_type,
28 char **out_raw_encoded_str);
30 da_bool_t is_base64_encoded_word(const char *in_str)
32 const char *haystack = DA_NULL;
33 char first_needle[8] = {0,};
34 char second_needle[8] = {0,};
35 char *found_str = DA_NULL;
38 DA_LOGE("input string is NULL");
43 if (haystack[0] == '"') {
44 snprintf(first_needle, sizeof(first_needle), "%s", "\"=?"); // "=?
45 snprintf(second_needle, sizeof(second_needle), "%s", "?=\""); // ?="
47 snprintf(first_needle, sizeof(first_needle), "%s", "=?"); // =?
48 snprintf(second_needle, sizeof(second_needle), "%s", "?="); // ?=
51 found_str = strstr(haystack, first_needle);
53 if (found_str == haystack) {
54 haystack = haystack + strlen(haystack) - strlen(second_needle);
55 if(!strcmp(haystack, second_needle))
62 da_ret_t decode_base64_encoded_str(const char *in_encoded_str,
63 char **out_decoded_ascii_str)
65 da_ret_t ret = DA_RESULT_OK;
66 const char *org_str = DA_NULL;
67 char *charset_type = NULL;
68 char encoding_type = '\0';
69 char *raw_encoded_str = NULL;
70 char *decoded_str = NULL;
71 const gchar *g_encoded_text = NULL;
72 guchar *g_decoded_text = NULL;
73 gsize g_decoded_text_len = 0;
75 DA_SECURE_LOGD("input str = [%s]", in_encoded_str);
77 org_str = in_encoded_str;
79 DA_LOGE("Input string is NULL");
80 ret = DA_ERR_INVALID_ARGUMENT;
84 ret = _parsing_base64_encoded_str(org_str, &charset_type,
85 &encoding_type, &raw_encoded_str);
86 if(ret != DA_RESULT_OK) {
90 if(encoding_type != 'B') {
91 DA_LOGE("Encoded Word is not encoded with Base64, but %c. We can only handle Base64.", encoding_type);
92 ret = DA_ERR_INVALID_ARGUMENT;
99 * typedef unsigned char guchar;
102 g_encoded_text = (const gchar*)raw_encoded_str;
103 g_decoded_text = g_base64_decode(g_encoded_text, &g_decoded_text_len);
106 DA_SECURE_LOGD("g_decoded_text = [%s]", g_decoded_text);
107 decoded_str = (char*)calloc(1, g_decoded_text_len+1);
109 DA_LOGE("DA_ERR_FAIL_TO_MEMALLOC");
110 ret = DA_ERR_FAIL_TO_MEMALLOC;
113 memcpy(decoded_str, g_decoded_text, g_decoded_text_len);
116 DA_SECURE_LOGD("decoded_str = [%s]", decoded_str);
119 *out_decoded_ascii_str = decoded_str;
124 if(raw_encoded_str) {
125 free(raw_encoded_str);
126 raw_encoded_str = NULL;
129 g_free(g_decoded_text);
134 da_ret_t _parsing_base64_encoded_str(const char *in_encoded_str,
135 char **out_charset_type,
136 char *out_encoding_type,
137 char **out_raw_encoded_str)
139 da_ret_t ret = DA_RESULT_OK;
140 const char *org_str = DA_NULL; // e.g. =?UTF-8?B?7Jew7JWE7JmA7IKs7J6QLmpwZw==?=
141 char *charset_type = NULL; // e.g. UTF-8
142 char encoding_type = '\0'; // e.g. B (means Base64)
143 char *raw_encoded_str = NULL; // e.g. 7Jew7JWE7JmA7IKs7J6QLmpwZw==
144 char *haystack = DA_NULL;
145 char needle[8] = {0,};
146 char *wanted_str = DA_NULL;
147 int wanted_str_len = 0;
148 char *wanted_str_start = DA_NULL;
149 char *wanted_str_end = DA_NULL;
151 org_str = in_encoded_str;
153 DA_LOGE("Input string is NULL");
154 ret = DA_ERR_INVALID_ARGUMENT;
159 haystack = (char*)org_str;
160 snprintf(needle, sizeof(needle), "=?");
161 wanted_str_end = strstr(haystack, needle);
162 if (!wanted_str_end) {
163 DA_LOGE("DA_ERR_INVALID_ARGUMENT");
164 ret = DA_ERR_INVALID_ARGUMENT;
167 wanted_str = wanted_str_end + strlen(needle);
171 haystack = wanted_str_start = wanted_str;
173 wanted_str_end = strchr(haystack, needle[0]);
174 if (!wanted_str_end) {
175 DA_LOGE("DA_ERR_INVALID_ARGUMENT");
176 ret = DA_ERR_INVALID_ARGUMENT;
179 wanted_str_len = wanted_str_end - wanted_str_start + 1;
180 wanted_str = (char*)calloc(1, wanted_str_len+1);
182 DA_LOGE("DA_ERR_FAIL_TO_MEMALLOC");
183 ret = DA_ERR_FAIL_TO_MEMALLOC;
186 snprintf(wanted_str, wanted_str_len+1, "%s", wanted_str_start);
187 charset_type = wanted_str;
188 wanted_str = DA_NULL;
191 DA_LOGV("charset [%s]", charset_type);
195 encoding_type = *(++wanted_str_end);
196 DA_LOGV("encoding [%c]", encoding_type);
198 // for raw encoded str
199 haystack = wanted_str_start = wanted_str_end + 1;
200 snprintf(needle, sizeof(needle), "?=");
201 wanted_str_end = strstr(haystack, needle);
202 if (!wanted_str_end) {
203 DA_LOGE("DA_ERR_INVALID_ARGUMENT");
204 ret = DA_ERR_INVALID_ARGUMENT;
207 wanted_str_len = wanted_str_end - wanted_str_start + 1;
208 wanted_str = (char*)calloc(1, wanted_str_len+1);
210 DA_LOGE("DA_ERR_FAIL_TO_MEMALLOC");
211 ret = DA_ERR_FAIL_TO_MEMALLOC;
214 snprintf(wanted_str, wanted_str_len+1, "%s", wanted_str_start);
215 raw_encoded_str = wanted_str;
219 DA_SECURE_LOGD("raw encoded str [%s]", raw_encoded_str);
222 if (ret != DA_RESULT_OK) {
228 *out_charset_type = charset_type;
229 *out_encoding_type = encoding_type;
230 *out_raw_encoded_str = raw_encoded_str;
234 void decode_url_encoded_str(const char *in_encoded_str, char **out_str)
236 CURL* curl = curl_easy_init();
238 *out_str = curl_easy_unescape(curl, in_encoded_str, strlen(in_encoded_str), NULL);
241 DA_LOGE("CURLE_OUT_OF_MEMORY");
245 curl_easy_cleanup(curl);