revise packaging
[platform/core/pim/contacts-service.git] / common / ctsvc_localize_jp.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <unicode/ustring.h>
21 #include <unicode/unorm.h>
22 #include <unicode/ucol.h>
23 #include <unicode/uset.h>
24
25 #include "ctsvc_internal.h"
26 #include "ctsvc_normalize.h"
27 #include "ctsvc_localize.h"
28 #include "ctsvc_localize_utils.h"
29
30 #include "ctsvc_localize_jp.h"
31
32 /* japanese - katakana */
33 #define CTSVC_JAPANESE_KATAKANA_START   0x30A0
34 #define CTSVC_JAPANESE_KATAKANA_END     0x30FF
35
36 /* japanese - katakana phonetic extensions */
37 #define CTSVC_JAPANESE_KATAKANA_PHONETIC_EXTENSIONS_START 0x31F0
38 #define CTSVC_JAPANESE_KATAKANA_PHONETIC_EXTENSIONS_END 0x31FF
39
40 /* japanese - halfwidth and fullwidth forms */
41 #define CTSVC_JAPANESE_HALFWIDTH_AND_FULLWIDTH_FORMS_START 0xFF00
42 #define CTSVC_JAPANESE_HALFWIDTH_AND_FULLWIDTH_FORMS_END 0xFFEF
43
44 /* japanese - halfwidth and fullwidth forms */
45 #define CTSVC_ASCII_HALFWIDTH_AND_FULLWIDTH_FORMS_START 0xFF01
46 #define CTSVC_ASCII_HALFWIDTH_AND_FULLWIDTH_FORMS_END 0xFF5E
47
48 /* japanese - hiragana */
49 #define CTSVC_JAPANESE_HIRAGANA_START 0x3040
50 #define CTSVC_JAPANESE_HIRAGANA_END 0x309F
51
52
53 static const unsigned char japanese_halfwidth_katakana_to_hiragana[] = { /* 0xff66 - 0xff9d */
54         0x92, 0x41, 0x43, 0x45, 0x47, 0x49, 0x83, 0x85, 0x87, 0x63,
55         0x00, 0x42, 0x44, 0x46, 0x48, 0x4A, 0x4B, 0x4D, 0x4F, 0x51,
56         0x53, 0x55, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x61, 0x64, 0x66,
57         0x68, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x72, 0x75, 0x78,
58         0x7B, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x84, 0x86, 0x88, 0x89,
59         0x8A, 0x8B, 0x8C, 0x8D, 0x8F, 0x93};
60
61 static const unsigned char japanese_halfwidth_katakana_sonant_to_hiragana[] = { /* 0xff76 - 0xff89 */
62         0x4C, 0x4E, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5A, 0x5C, 0x5E,
63         0x60, 0x62, 0x65, 0x67, 0x69, 0x70, 0x73, 0x76, 0x79, 0x7C};
64
65 static const unsigned char japanese_halfwidth_katakana_half_dullness_to_hiragana[] = { /* 0xff8a - 0xff8e */
66         0x71, 0x74, 0x77, 0x7A, 0x7D};
67
68
69 static inline bool is_japanese(UChar src)
70 {
71         if (CTSVC_COMPARE_BETWEEN(CTSVC_JAPANESE_KATAKANA_START, src, CTSVC_JAPANESE_KATAKANA_END)
72                         || CTSVC_COMPARE_BETWEEN(CTSVC_JAPANESE_KATAKANA_PHONETIC_EXTENSIONS_START, src, CTSVC_JAPANESE_KATAKANA_PHONETIC_EXTENSIONS_END)
73                         || CTSVC_COMPARE_BETWEEN(CTSVC_JAPANESE_HALFWIDTH_AND_FULLWIDTH_FORMS_START, src, CTSVC_JAPANESE_HALFWIDTH_AND_FULLWIDTH_FORMS_END)
74                         || CTSVC_COMPARE_BETWEEN(CTSVC_JAPANESE_HIRAGANA_START, src, CTSVC_JAPANESE_HIRAGANA_END)) {
75                 return true;
76         } else {
77                 return false;
78         }
79 }
80
81
82 int ctsvc_convert_japanese_to_hiragana_unicode(UChar *src, UChar *dest, int dest_size)
83 {
84         int i, j = 0, len = 0;
85
86         len = u_strlen(src);
87
88         for (i = 0; i < len; i++) {
89                 int unicode_value1 = 0;
90                 int unicode_value2 = 0;
91
92                 unicode_value2 = (0xFF & (src[i]));
93
94                 if (CTSVC_COMPARE_BETWEEN(CTSVC_JAPANESE_KATAKANA_START, src[i], CTSVC_JAPANESE_KATAKANA_END)) {
95                         unicode_value1 = 0x30;
96                         if ((0xa1 <= unicode_value2 && unicode_value2 <= 0xef)
97                                         || (unicode_value2 == 0xF2 || unicode_value2 == 0xF3)) {
98                                 unicode_value2 -= 0x60;
99                                 dest[j] = unicode_value1 << 8 | unicode_value2;
100                         } else {
101                                 dest[j] = src[i];
102                         }
103                 } else if (CTSVC_COMPARE_BETWEEN(CTSVC_JAPANESE_HALFWIDTH_AND_FULLWIDTH_FORMS_START,
104                                         src[i], CTSVC_JAPANESE_HALFWIDTH_AND_FULLWIDTH_FORMS_END)) {
105                         unicode_value1 = 0x30;
106                         if (i+1 < len && (0xFF & (src[i+1])) == 0x9E
107                                         && 0x76 <= unicode_value2 && unicode_value2 <= 0x89) {
108                                 unicode_value2 = japanese_halfwidth_katakana_sonant_to_hiragana[unicode_value2 - 0x76];
109                                 dest[j] = unicode_value1 << 8 | unicode_value2;
110                                 i++;
111                         } else if (i+1 < len && (0xFF & (src[i])) == 0x9F
112                                         && 0x8a <= unicode_value2 && unicode_value2 <= 0x8e) {
113                                 unicode_value2 = japanese_halfwidth_katakana_half_dullness_to_hiragana[unicode_value2 - 0x8a];
114                                 dest[j] = unicode_value1 << 8 | unicode_value2;
115                                 i++;
116                         } else if (0x66 <= unicode_value2 && unicode_value2 <= 0x9d) {
117                                 unicode_value2 = japanese_halfwidth_katakana_to_hiragana[unicode_value2 - 0x66];
118                                 dest[j] = unicode_value1 << 8 | unicode_value2;
119                         } else {
120                                 dest[j] = src[i];
121                         }
122                 } else if (CTSVC_COMPARE_BETWEEN(CTSVC_ASCII_HALFWIDTH_AND_FULLWIDTH_FORMS_START,
123                                         src[i], CTSVC_ASCII_HALFWIDTH_AND_FULLWIDTH_FORMS_END)) {
124                         unicode_value1 = 0x00;
125                         unicode_value2 = unicode_value2 - 0x20;
126                         dest[j] = unicode_value1 << 8 | unicode_value2;
127                 } else {
128                         dest[j] = src[i];
129                 }
130                 j++;
131         }
132
133         dest[j] = 0x0;
134
135         return j;
136 }
137
138 int ctsvc_convert_japanese_to_hiragana(const char *src, char **dest)
139 {
140         UChar *tmp_result = NULL;
141         UChar *result = NULL;
142         UErrorCode status = 0;
143         int32_t size;
144
145         u_strFromUTF8(NULL, 0, &size, src, strlen(src), &status);
146         if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
147                 /* LCOV_EXCL_START */
148                 ERR("u_strFromUTF8 to get the dest length Fail(%s)", u_errorName(status));
149                 return CONTACTS_ERROR_SYSTEM;
150                 /* LCOV_EXCL_STOP */
151         }
152         status = U_ZERO_ERROR;
153         tmp_result = calloc(1, sizeof(UChar) * (size + 1));
154         if (NULL == tmp_result) {
155                 /* LCOV_EXCL_START */
156                 ERR("calloc() Fail");
157                 return CONTACTS_ERROR_OUT_OF_MEMORY;
158                 /* LCOV_EXCL_STOP */
159         }
160
161         u_strFromUTF8(tmp_result, size + 1, NULL, src, -1, &status);
162         if (U_FAILURE(status)) {
163                 /* LCOV_EXCL_START */
164                 ERR("u_strFromUTF8 Fail(%s)", u_errorName(status));
165                 free(tmp_result);
166                 return CONTACTS_ERROR_SYSTEM;
167                 /* LCOV_EXCL_STOP */
168         }
169
170         result = calloc(1, sizeof(UChar) * (size + 1));
171         if (NULL == result) {
172                 /* LCOV_EXCL_START */
173                 ERR("calloc() Fail");
174                 free(tmp_result);
175                 return CONTACTS_ERROR_OUT_OF_MEMORY;
176                 /* LCOV_EXCL_STOP */
177         }
178
179         ctsvc_convert_japanese_to_hiragana_unicode(tmp_result, result, size + 1);
180         u_strToUTF8(NULL, 0, &size, result, -1, &status);
181         if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
182                 /* LCOV_EXCL_START */
183                 ERR("u_strToUTF8 to get the dest length Fail(%s)", u_errorName(status));
184                 free(tmp_result);
185                 free(result);
186                 return CONTACTS_ERROR_SYSTEM;
187                 /* LCOV_EXCL_STOP */
188         }
189
190         status = U_ZERO_ERROR;
191         *dest = calloc(1, sizeof(char)*(size+1));
192         if (NULL == *dest) {
193                 /* LCOV_EXCL_START */
194                 ERR("calloc() Fail");
195                 free(tmp_result);
196                 free(result);
197                 return CONTACTS_ERROR_OUT_OF_MEMORY;
198                 /* LCOV_EXCL_STOP */
199         }
200
201         u_strToUTF8(*dest, size + 1, &size, result, -1, &status);
202         if (U_FAILURE(status)) {
203                 /* LCOV_EXCL_START */
204                 ERR("u_strToUTF8 Fail(%s)", u_errorName(status));
205                 free(tmp_result);
206                 free(result);
207                 free(*dest);
208                 *dest = NULL;
209                 return CONTACTS_ERROR_SYSTEM;
210                 /* LCOV_EXCL_STOP */
211         }
212
213         free(tmp_result);
214         free(result);
215         return CONTACTS_ERROR_NONE;
216 }
217