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.
21 #include "download-agent-encoding.h"
22 #include "download-agent-debug.h"
24 da_result_t _parsing_base64_encoded_str(const char *in_encoded_str,
25 char **out_charset_type,
26 char *out_encoding_type,
27 char **out_raw_encoded_str);
29 da_bool_t is_base64_encoded_word(const char *in_str)
31 const char *haystack = DA_NULL;
32 char first_needle[8] = {0,};
33 char second_needle[8] = {0,};
34 char *found_str = DA_NULL;
37 DA_LOG_ERR(Default, "input string is NULL");
42 if (haystack[0] == '"') {
43 snprintf(first_needle, sizeof(first_needle), "%s", "\"=?"); // "=?
44 snprintf(second_needle, sizeof(second_needle), "%s", "?=\""); // ?="
46 snprintf(first_needle, sizeof(first_needle), "%s", "=?"); // =?
47 snprintf(second_needle, sizeof(second_needle), "%s", "?="); // ?=
50 // DA_LOG(Default, "needle = [%s], haystack = [%s]", first_needle, haystack);
52 found_str = strstr(haystack, first_needle);
54 if (found_str == haystack) {
55 // DA_LOG(Default, "Input string is starting with %s", needle);
56 haystack = haystack + strlen(haystack) - strlen(second_needle);
57 // DA_LOG(Default, "second haystack is [%s]", haystack);
58 if(!strcmp(haystack, second_needle))
65 da_result_t decode_base64_encoded_str(const char *in_encoded_str,
66 char **out_decoded_ascii_str)
68 da_result_t ret = DA_RESULT_OK;
70 const char *org_str = DA_NULL;
71 char *charset_type = NULL;
72 char encoding_type = '\0';
73 char *raw_encoded_str = NULL;
74 char *decoded_str = NULL;
75 const gchar *g_encoded_text = NULL;
76 guchar *g_decoded_text = NULL;
77 gsize g_decoded_text_len = 0;
79 DA_LOG(Default, "input str = [%s]", in_encoded_str);
81 org_str = in_encoded_str;
83 DA_LOG_ERR(Default, "Input string is NULL");
84 ret = DA_ERR_INVALID_ARGUMENT;
88 ret = _parsing_base64_encoded_str(org_str, &charset_type,
89 &encoding_type, &raw_encoded_str);
90 if(ret != DA_RESULT_OK) {
94 // DA_LOG(Default, "charset = [%s], encoding = [%c], raw = [%s]", charset_type, encoding_type, raw_encoded_str);
96 if(encoding_type != 'B') {
97 DA_LOG_ERR(Default, "Encoded Word is not encoded with Base64, but %c. We can only handle Base64.", encoding_type);
98 ret = DA_ERR_INVALID_ARGUMENT;
104 * typedef char gchar;
105 * typedef unsigned char guchar;
108 g_encoded_text = (const gchar*)raw_encoded_str;
109 g_decoded_text = g_base64_decode(g_encoded_text, &g_decoded_text_len);
112 DA_LOG(Default, "g_decoded_text = [%s]", g_decoded_text);
113 decoded_str = (char*)calloc(1, g_decoded_text_len+1);
115 DA_LOG_ERR(Default, "DA_ERR_FAIL_TO_MEMALLOC");
116 ret = DA_ERR_FAIL_TO_MEMALLOC;
119 memcpy(decoded_str, g_decoded_text, g_decoded_text_len);
122 DA_LOG(Default, "decoded_str = [%s]", decoded_str);
125 *out_decoded_ascii_str = decoded_str;
132 if(raw_encoded_str) {
133 free(raw_encoded_str);
134 raw_encoded_str = NULL;
138 g_free(g_decoded_text);
145 da_result_t _parsing_base64_encoded_str(const char *in_encoded_str,
146 char **out_charset_type,
147 char *out_encoding_type,
148 char **out_raw_encoded_str)
150 da_result_t ret = DA_RESULT_OK;
152 const char *org_str = DA_NULL; // e.g. =?UTF-8?B?7Jew7JWE7JmA7IKs7J6QLmpwZw==?=
153 char *charset_type = NULL; // e.g. UTF-8
154 char encoding_type = '\0'; // e.g. B (means Base64)
155 char *raw_encoded_str = NULL; // e.g. 7Jew7JWE7JmA7IKs7J6QLmpwZw==
157 char *haystack = DA_NULL;
158 char needle[8] = {0,};
160 char *wanted_str = DA_NULL;
161 int wanted_str_len = 0;
162 char *wanted_str_start = DA_NULL;
163 char *wanted_str_end = DA_NULL;
165 org_str = in_encoded_str;
167 DA_LOG_ERR(Default, "Input string is NULL");
168 ret = DA_ERR_INVALID_ARGUMENT;
173 haystack = (char*)org_str;
174 snprintf(needle, sizeof(needle), "=?");
175 wanted_str_end = strstr(haystack, needle);
176 if (!wanted_str_end) {
177 DA_LOG_ERR(Default, "DA_ERR_INVALID_ARGUMENT");
178 ret = DA_ERR_INVALID_ARGUMENT;
181 wanted_str = wanted_str_end + strlen(needle);
182 DA_LOG(Default, "strip [%s]", wanted_str);
186 haystack = wanted_str_start = wanted_str;
188 wanted_str_end = strchr(haystack, needle[0]);
189 if (!wanted_str_end) {
190 DA_LOG_ERR(Default, "DA_ERR_INVALID_ARGUMENT");
191 ret = DA_ERR_INVALID_ARGUMENT;
194 wanted_str_len = wanted_str_end - wanted_str_start + 1;
195 wanted_str = (char*)calloc(1, wanted_str_len+1);
197 DA_LOG_ERR(Default, "DA_ERR_FAIL_TO_MEMALLOC");
198 ret = DA_ERR_FAIL_TO_MEMALLOC;
201 snprintf(wanted_str, wanted_str_len+1, "%s", wanted_str_start);
202 charset_type = wanted_str;
203 wanted_str = DA_NULL;
206 DA_LOG(Default, "charset [%s]", charset_type);
211 encoding_type = *(++wanted_str_end);
212 DA_LOG(Default, "encoding [%c]", encoding_type);
214 // for raw encoded str
215 haystack = wanted_str_start = wanted_str_end + 1;
216 snprintf(needle, sizeof(needle), "?=");
217 wanted_str_end = strstr(haystack, needle);
218 if (!wanted_str_end) {
219 DA_LOG_ERR(Default, "DA_ERR_INVALID_ARGUMENT");
220 ret = DA_ERR_INVALID_ARGUMENT;
223 wanted_str_len = wanted_str_end - wanted_str_start + 1;
224 wanted_str = (char*)calloc(1, wanted_str_len+1);
226 DA_LOG_ERR(Default, "DA_ERR_FAIL_TO_MEMALLOC");
227 ret = DA_ERR_FAIL_TO_MEMALLOC;
230 snprintf(wanted_str, wanted_str_len+1, "%s", wanted_str_start);
231 raw_encoded_str = wanted_str;
235 DA_LOG(Default, "raw encoded str [%s]", raw_encoded_str);
239 if (ret != DA_RESULT_OK) {
246 *out_charset_type = charset_type;
247 *out_encoding_type = encoding_type;
248 *out_raw_encoded_str = raw_encoded_str;
253 void decode_url_encoded_str(const char *in_encoded_str, char **out_str)
257 *out_str = calloc(1, strlen(in_encoded_str) + 1);
258 if (*out_str == NULL)
261 in = (char *)in_encoded_str;
267 if (sscanf(in, "%2x", &hex) <= 0) {
273 } else if (*in == '+') {