Fix coverity issue (INTEGER_OVERFLOW)
[platform/core/multimedia/libmm-fileinfo.git] / utils / mm_file_util_string.c
1 /*
2  * libmm-fileinfo
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <unicode/ucsdet.h>
26 #include "mm_file_debug.h"
27 #include "mm_file_utils.h"
28
29 char *mmfile_get_charset(const char *str)
30 {
31         UCharsetDetector *ucsd = NULL;
32         const UCharsetMatch *ucm = NULL;
33         UErrorCode status = U_ZERO_ERROR;
34
35         const char *charset = NULL;
36         char *ret_charset = NULL;
37
38         ucsd = ucsdet_open(&status);
39         if (U_FAILURE(status)) {
40                 debug_error(DEBUG, "fail to ucsdet_open\n");
41                 return NULL;
42         }
43
44         ucsdet_enableInputFilter(ucsd, true);
45
46         ucsdet_setText(ucsd, str, strlen(str), &status);
47         if (U_FAILURE(status)) {
48                 debug_error(DEBUG, "fail to ucsdet_setText\n");
49                 goto done;
50         }
51
52         ucm = ucsdet_detect(ucsd, &status);
53         if (U_FAILURE(status)) {
54                 debug_error(DEBUG, "fail to ucsdet_detect\n");
55                 goto done;
56         }
57
58         if (ucm == NULL) {
59                 debug_error(DEBUG, "fail to ucsdet_detect\n");
60                 goto done;
61         }
62
63         charset = ucsdet_getName(ucm, &status);
64         if (U_FAILURE(status)) {
65                 debug_error(DEBUG, "fail to ucsdet_getName\n");
66                 charset = NULL;
67                 goto done;
68         }
69
70 done:
71
72         if (charset != NULL)
73                 ret_charset = strdup(charset);
74
75         ucsdet_close(ucsd);
76
77         return ret_charset;
78 }
79
80
81 char *mmfile_convert_to_utf8(const char *str, ssize_t len, const char *from_codeset)
82 {
83         char *result = NULL;
84         GError *err = NULL;
85         char **split = NULL;
86
87         mm_file_retvm_if_fails(DEBUG, len != 0, NULL);
88
89         /*if both to_codeset and from_codeset are same, return duplicated string.*/
90         if (g_strcmp0(MMFILE_CODESET_UTF8, from_codeset) == 0)
91                 return g_strndup(str, len);
92
93         result = g_convert(str, len, MMFILE_CODESET_UTF8, from_codeset, NULL, NULL, &err);
94
95         /*if converting failed, return null string.*/
96         if (!result) {
97                 debug_warning(RELEASE, "text encoding failed.[%s][%zd]\n", str, len);
98                 if (err != NULL) {
99                         debug_warning(DEBUG, "Error msg [%s]", err->message);
100                         g_error_free(err);
101                 }
102                 return NULL;
103         }
104
105         /* Remove '\r' */
106         split = g_strsplit(result, "\r", -1);
107         g_free(result);
108
109         result = g_strjoinv(NULL, split);
110         g_strfreev(split);
111
112         return result;
113 }