Debian Codegraph @

lib/encoding.c

raw symbols SCIP
1 /*
2 Copyright(C) 2018 Brazil
3 Copyright(C) 2022 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <stdio.h>
21
22 #include "grn.h"
23 #include "grn_ctx.h"
24 #include "grn_encoding.h"
25 #include "grn_windows.h"
26
27 #include <string.h>
28
29 #ifdef WIN32
30 static const char *
31 grn_encoding_convert(grn_ctx *ctx,
32 const char *context,
33 UINT from_code_page,
34 UINT to_code_page,
35 const char *from_string,
36 ssize_t from_string_size,
37 size_t *converted_string_size)
38 {
39 char *converted_string = NULL;
40
41 if (from_string_size < 0) {
42 from_string_size = strlen(from_string);
43 }
44
45 if (from_code_page == to_code_page) {
46 goto exit;
47 }
48
49 {
50 WCHAR *utf16_string;
51 DWORD n_utf16_chars;
52 size_t converted_string_size_;
53
54 n_utf16_chars = MultiByteToWideChar(from_code_page,
55 0,
56 from_string,
57 from_string_size,
58 NULL,
59 0);
60 if (n_utf16_chars == 0) {
61 SERR("%s failed to estimate the number of UTF-16 characters",
62 context);
63 goto exit;
64 }
65 utf16_string = GRN_MALLOCN(WCHAR, n_utf16_chars);
66 n_utf16_chars = MultiByteToWideChar(from_code_page,
67 0,
68 from_string,
69 from_string_size,
70 utf16_string,
71 n_utf16_chars);
72 if (n_utf16_chars == 0) {
73 SERR("%s failed to convert to UTF-16 characters",
74 context);
75 GRN_FREE(utf16_string);
76 goto exit;
77 }
78
79 converted_string_size_ = WideCharToMultiByte(to_code_page,
80 0,
81 utf16_string,
82 n_utf16_chars,
83 NULL,
84 0,
85 NULL,
86 NULL);
87 if (converted_string_size_ == 0) {
88 SERR("%s failed to estimate required buffer size for converted string",
89 context);
90 GRN_FREE(utf16_string);
91 goto exit;
92 }
93
94 converted_string = GRN_MALLOCN(char, converted_string_size_ + 1);
95 converted_string_size_ = WideCharToMultiByte(to_code_page,
96 0,
97 utf16_string,
98 n_utf16_chars,
99 converted_string,
100 converted_string_size_,
101 NULL,
102 NULL);
103 GRN_FREE(utf16_string);
104 if (converted_string_size_ == 0) {
105 SERR("%s failed to estimate required buffer size for converted string",
106 context);
107 GRN_FREE(converted_string);
108 converted_string = NULL;
109 goto exit;
110 }
111 converted_string[converted_string_size_] = '\0';
112 if (converted_string_size) {
113 *converted_string_size = converted_string_size_;
114 }
115 }
116
117 exit :
118 if (!converted_string) {
119 converted_string = GRN_MALLOCN(char, from_string_size + 1);
120 if (converted_string) {
121 grn_memcpy(converted_string, from_string, from_string_size);
122 converted_string[from_string_size] = '\0';
123 if (converted_string_size) {
124 *converted_string_size = from_string_size;
125 }
126 } else {
127 ERR(ctx->rc,
128 "%s failed to allocate a buffer for converted string",
129 context);
130 if (converted_string_size) {
131 *converted_string_size = 0;
132 }
133 }
134 }
135
136 return converted_string;
137 }
138
139 const char *
140 grn_encoding_convert_to_locale(grn_ctx *ctx,
141 const char *grn_encoding_string,
142 ssize_t grn_encoding_string_size,
143 size_t *converted_string_size)
144 {
145 return grn_encoding_convert(ctx,
146 "[encoding][convert][grn->locale]",
147 grn_windows_encoding_to_code_page(ctx->encoding),
148 CP_ACP,
149 grn_encoding_string,
150 grn_encoding_string_size,
151 converted_string_size);
152 }
153
154 const char *
155 grn_encoding_convert_to_utf8(grn_ctx *ctx,
156 const char *grn_encoding_string,
157 ssize_t grn_encoding_string_size,
158 size_t *converted_string_size)
159 {
160 return grn_encoding_convert(ctx,
161 "[encoding][convert][grn->utf8]",
162 grn_windows_encoding_to_code_page(ctx->encoding),
163 CP_UTF8,
164 grn_encoding_string,
165 grn_encoding_string_size,
166 converted_string_size);
167 }
168
169 const char *
170 grn_encoding_convert_from_locale(grn_ctx *ctx,
171 const char *locale_string,
172 ssize_t locale_string_size,
173 size_t *converted_string_size)
174 {
175 return grn_encoding_convert(ctx,
176 "[encoding][convert][locale->grn]",
177 CP_ACP,
178 grn_windows_encoding_to_code_page(ctx->encoding),
179 locale_string,
180 locale_string_size,
181 converted_string_size);
182 }
183
184 const char *
185 grn_encoding_convert_from_utf8(grn_ctx *ctx,
186 const char *utf8_string,
187 ssize_t utf8_string_size,
188 size_t *converted_string_size)
189 {
190 return grn_encoding_convert(ctx,
191 "[encoding][convert][utf8->grn]",
192 CP_UTF8,
193 grn_windows_encoding_to_code_page(ctx->encoding),
194 utf8_string,
195 utf8_string_size,
196 converted_string_size);
197 }
198
199 const char *
200 grn_encoding_convert_to_utf8_from_locale(grn_ctx *ctx,
201 const char *locale_string,
202 ssize_t locale_string_size,
203 size_t *converted_string_size)
204 {
205 return grn_encoding_convert(ctx,
206 "[encoding][convert][locale->utf8]",
207 CP_ACP,
208 CP_UTF8,
209 locale_string,
210 locale_string_size,
211 converted_string_size);
212 }
213
214 const char *
215 grn_encoding_convert_to_locale_from_utf8(grn_ctx *ctx,
216 const char *utf8_string,
217 ssize_t utf8_string_size,
218 size_t *converted_string_size)
219 {
220 return grn_encoding_convert(ctx,
221 "[encoding][convert][utf8->locale]",
222 CP_UTF8,
223 CP_ACP,
224 utf8_string,
225 utf8_string_size,
226 converted_string_size);
227 }
228
229 void
230 grn_encoding_converted_free(grn_ctx *ctx, const char *converted_string)
231 {
232 GRN_FREE((char *)converted_string);
233 }
234 #else /* WIN32 */
235 const char *
236 grn_encoding_convert_to_locale(grn_ctx *ctx,
237 const char *grn_encoding_string,
238 ssize_t grn_encoding_string_size,
239 size_t *converted_string_size)
240 {
241 if (converted_string_size) {
242 if (grn_encoding_string_size < 0) {
243 *converted_string_size = strlen(grn_encoding_string);
244 } else {
245 *converted_string_size = (size_t)grn_encoding_string_size;
246 }
247 }
248 return grn_encoding_string;
249 }
250
251 const char *
252 grn_encoding_convert_to_utf8(grn_ctx *ctx,
253 const char *grn_encoding_string,
254 ssize_t grn_encoding_string_size,
255 size_t *converted_string_size)
256 {
257 if (converted_string_size) {
258 if (grn_encoding_string_size < 0) {
259 *converted_string_size = strlen(grn_encoding_string);
260 } else {
261 *converted_string_size = (size_t)grn_encoding_string_size;
262 }
263 }
264 return grn_encoding_string;
265 }
266
267 const char *
268 grn_encoding_convert_from_locale(grn_ctx *ctx,
269 const char *locale_string,
270 ssize_t locale_string_size,
271 size_t *converted_string_size)
272 {
273 if (converted_string_size) {
274 if (locale_string_size < 0) {
275 *converted_string_size = strlen(locale_string);
276 } else {
277 *converted_string_size = (size_t)locale_string_size;
278 }
279 }
280 return locale_string;
281 }
282
283 const char *
284 grn_encoding_convert_from_utf8(grn_ctx *ctx,
285 const char *utf8_string,
286 ssize_t utf8_string_size,
287 size_t *converted_string_size)
288 {
289 if (converted_string_size) {
290 if (utf8_string_size < 0) {
291 *converted_string_size = strlen(utf8_string);
292 } else {
293 *converted_string_size = (size_t)utf8_string_size;
294 }
295 }
296 return utf8_string;
297 }
298
299 const char *
300 grn_encoding_convert_to_utf8_from_locale(grn_ctx *ctx,
301 const char *locale_string,
302 ssize_t locale_string_size,
303 size_t *converted_string_size)
304 {
305 if (converted_string_size) {
306 if (locale_string_size < 0) {
307 *converted_string_size = strlen(locale_string);
308 } else {
309 *converted_string_size = (size_t)locale_string_size;
310 }
311 }
312 return locale_string;
313 }
314
315 const char *
316 grn_encoding_convert_to_locale_from_utf8(grn_ctx *ctx,
317 const char *utf8_string,
318 ssize_t utf8_string_size,
319 size_t *converted_string_size)
320 {
321 if (converted_string_size) {
322 if (utf8_string_size < 0) {
323 *converted_string_size = strlen(utf8_string);
324 } else {
325 *converted_string_size = (size_t)utf8_string_size;
326 }
327 }
328 return utf8_string;
329 }
330
331 void
332 grn_encoding_converted_free(grn_ctx *ctx, const char *converted_string)
333 {
334 }
335 #endif /* WIN32 */