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-debug.h"
22 #include "download-agent-encoding.h"
24 da_ret_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_LOGE("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 found_str = strstr(haystack, first_needle);
52 if (found_str == haystack) {
53 haystack = haystack + strlen(haystack) - strlen(second_needle);
54 if(!strcmp(haystack, second_needle))
61 da_ret_t decode_base64_encoded_str(const char *in_encoded_str,
62 char **out_decoded_ascii_str)
64 da_ret_t ret = DA_RESULT_OK;
65 const char *org_str = DA_NULL;
66 char *charset_type = NULL;
67 char encoding_type = '\0';
68 char *raw_encoded_str = NULL;
69 char *decoded_str = NULL;
70 const gchar *g_encoded_text = NULL;
71 guchar *g_decoded_text = NULL;
72 gsize g_decoded_text_len = 0;
74 DA_SECURE_LOGD("input str = [%s]", in_encoded_str);
76 org_str = in_encoded_str;
78 DA_LOGE("Input string is NULL");
79 ret = DA_ERR_INVALID_ARGUMENT;
83 ret = _parsing_base64_encoded_str(org_str, &charset_type,
84 &encoding_type, &raw_encoded_str);
85 if(ret != DA_RESULT_OK) {
89 if(encoding_type != 'B') {
90 DA_LOGE("Encoded Word is not encoded with Base64, but %c. We can only handle Base64.", encoding_type);
91 ret = DA_ERR_INVALID_ARGUMENT;
98 * typedef unsigned char guchar;
101 g_encoded_text = (const gchar*)raw_encoded_str;
102 g_decoded_text = g_base64_decode(g_encoded_text, &g_decoded_text_len);
105 DA_SECURE_LOGD("g_decoded_text = [%s]", g_decoded_text);
106 decoded_str = (char*)calloc(1, g_decoded_text_len+1);
108 DA_LOGE("DA_ERR_FAIL_TO_MEMALLOC");
109 ret = DA_ERR_FAIL_TO_MEMALLOC;
112 memcpy(decoded_str, g_decoded_text, g_decoded_text_len);
115 DA_SECURE_LOGD("decoded_str = [%s]", decoded_str);
118 *out_decoded_ascii_str = decoded_str;
123 if(raw_encoded_str) {
124 free(raw_encoded_str);
125 raw_encoded_str = NULL;
128 g_free(g_decoded_text);
133 da_ret_t _parsing_base64_encoded_str(const char *in_encoded_str,
134 char **out_charset_type,
135 char *out_encoding_type,
136 char **out_raw_encoded_str)
138 da_ret_t ret = DA_RESULT_OK;
139 const char *org_str = DA_NULL; // e.g. =?UTF-8?B?7Jew7JWE7JmA7IKs7J6QLmpwZw==?=
140 char *charset_type = NULL; // e.g. UTF-8
141 char encoding_type = '\0'; // e.g. B (means Base64)
142 char *raw_encoded_str = NULL; // e.g. 7Jew7JWE7JmA7IKs7J6QLmpwZw==
143 char *haystack = DA_NULL;
144 char needle[8] = {0,};
145 char *wanted_str = DA_NULL;
146 int wanted_str_len = 0;
147 char *wanted_str_start = DA_NULL;
148 char *wanted_str_end = DA_NULL;
150 org_str = in_encoded_str;
152 DA_LOGE("Input string is NULL");
153 ret = DA_ERR_INVALID_ARGUMENT;
158 haystack = (char*)org_str;
159 snprintf(needle, sizeof(needle), "=?");
160 wanted_str_end = strstr(haystack, needle);
161 if (!wanted_str_end) {
162 DA_LOGE("DA_ERR_INVALID_ARGUMENT");
163 ret = DA_ERR_INVALID_ARGUMENT;
166 wanted_str = wanted_str_end + strlen(needle);
170 haystack = wanted_str_start = wanted_str;
172 wanted_str_end = strchr(haystack, needle[0]);
173 if (!wanted_str_end) {
174 DA_LOGE("DA_ERR_INVALID_ARGUMENT");
175 ret = DA_ERR_INVALID_ARGUMENT;
178 wanted_str_len = wanted_str_end - wanted_str_start + 1;
179 wanted_str = (char*)calloc(1, wanted_str_len+1);
181 DA_LOGE("DA_ERR_FAIL_TO_MEMALLOC");
182 ret = DA_ERR_FAIL_TO_MEMALLOC;
185 snprintf(wanted_str, wanted_str_len+1, "%s", wanted_str_start);
186 charset_type = wanted_str;
187 wanted_str = DA_NULL;
190 DA_LOGV("charset [%s]", charset_type);
194 encoding_type = *(++wanted_str_end);
195 DA_LOGV("encoding [%c]", encoding_type);
197 // for raw encoded str
198 haystack = wanted_str_start = wanted_str_end + 1;
199 snprintf(needle, sizeof(needle), "?=");
200 wanted_str_end = strstr(haystack, needle);
201 if (!wanted_str_end) {
202 DA_LOGE("DA_ERR_INVALID_ARGUMENT");
203 ret = DA_ERR_INVALID_ARGUMENT;
206 wanted_str_len = wanted_str_end - wanted_str_start + 1;
207 wanted_str = (char*)calloc(1, wanted_str_len+1);
209 DA_LOGE("DA_ERR_FAIL_TO_MEMALLOC");
210 ret = DA_ERR_FAIL_TO_MEMALLOC;
213 snprintf(wanted_str, wanted_str_len+1, "%s", wanted_str_start);
214 raw_encoded_str = wanted_str;
218 DA_SECURE_LOGD("raw encoded str [%s]", raw_encoded_str);
221 if (ret != DA_RESULT_OK) {
227 *out_charset_type = charset_type;
228 *out_encoding_type = encoding_type;
229 *out_raw_encoded_str = raw_encoded_str;
233 void decode_url_encoded_str(const char *in_encoded_str, char **out_str)
237 *out_str = calloc(1, strlen(in_encoded_str) + 1);
238 if (*out_str == NULL)
241 in = (char *)in_encoded_str;
247 if (sscanf(in, "%2x", &hex) <= 0) {
253 } else if (*in == '+') {