/src/dcmtk/oficonv/libsrc/oficonv_iconv.c
Line | Count | Source |
1 | | /*- |
2 | | * Copyright (c) 2003 Citrus Project, |
3 | | * Copyright (c) 2009, 2010 Gabor Kovesdan <gabor@FreeBSD.org>, |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * 1. Redistributions of source code must retain the above copyright |
10 | | * notice, this list of conditions and the following disclaimer. |
11 | | * 2. Redistributions in binary form must reproduce the above copyright |
12 | | * notice, this list of conditions and the following disclaimer in the |
13 | | * documentation and/or other materials provided with the distribution. |
14 | | * |
15 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 | | * SUCH DAMAGE. |
26 | | */ |
27 | | |
28 | | #include "dcmtk/config/osconfig.h" |
29 | | #include "dcmtk/oficonv/iconv.h" |
30 | | |
31 | | #ifdef HAVE_SYS_QUEUE_H |
32 | | #include <sys/queue.h> |
33 | | #else |
34 | | #include "dcmtk/oficonv/queue.h" |
35 | | #endif |
36 | | |
37 | | #include <sys/types.h> |
38 | | #include <errno.h> |
39 | | #include <limits.h> |
40 | | #include <stdbool.h> |
41 | | #include <stdlib.h> |
42 | | #include <string.h> |
43 | | #include <stdio.h> |
44 | | |
45 | | #ifdef HAVE_LANGINFO_H |
46 | | #include <langinfo.h> |
47 | | #endif |
48 | | |
49 | | #include <locale.h> |
50 | | |
51 | | #include "citrus_types.h" |
52 | | #include "citrus_module.h" |
53 | | #include "citrus_esdb.h" |
54 | | #include "citrus_hash.h" |
55 | | #include "citrus_iconv.h" |
56 | | #include "citrus_csmapper.h" |
57 | | #include "oficonv_strlcpy.h" |
58 | | #include "oficonv_strcasestr.h" |
59 | | |
60 | 0 | #define ISBADF(_h_) (!(_h_) || (_h_) == (iconv_t)-1) |
61 | | |
62 | | char *my_strndup(const char *str, size_t chars) |
63 | 0 | { |
64 | 0 | char *buffer; |
65 | 0 | size_t n; |
66 | |
|
67 | 0 | buffer = (char *) malloc(chars +1); |
68 | 0 | if (buffer) |
69 | 0 | { |
70 | 0 | for (n = 0; ((n < chars) && (str[n] != 0)) ; n++) buffer[n] = str[n]; |
71 | 0 | buffer[n] = 0; |
72 | 0 | } |
73 | |
|
74 | 0 | return buffer; |
75 | 0 | } |
76 | | |
77 | | int _iconv_version = _ICONV_VERSION; |
78 | | |
79 | | iconv_t _iconv_open(const char *out, const char *in, |
80 | | struct _citrus_iconv *prealloc); |
81 | | |
82 | | iconv_t |
83 | | _iconv_open(const char *out, const char *in, struct _citrus_iconv *handle) |
84 | 0 | { |
85 | 0 | const char *out_slashes; |
86 | 0 | char *out_noslashes; |
87 | 0 | int ret; |
88 | | |
89 | | /* |
90 | | * Remove anything following a //, as these are options (like |
91 | | * //ignore, //translate, etc) and we just don't handle them. |
92 | | * This is for compatibility with software that uses these |
93 | | * blindly. |
94 | | */ |
95 | 0 | out_slashes = strstr(out, "//"); |
96 | 0 | if (out_slashes != NULL) { |
97 | 0 | out_noslashes = my_strndup(out, out_slashes - out); |
98 | 0 | if (out_noslashes == NULL) { |
99 | 0 | errno = ENOMEM; |
100 | 0 | return ((iconv_t)-1); |
101 | 0 | } |
102 | 0 | ret = _citrus_iconv_open(&handle, in, out_noslashes); |
103 | 0 | free(out_noslashes); |
104 | 0 | } else { |
105 | 0 | ret = _citrus_iconv_open(&handle, in, out); |
106 | 0 | } |
107 | | |
108 | 0 | if (ret) { |
109 | 0 | errno = ret == ENOENT ? EINVAL : ret; |
110 | 0 | return ((iconv_t)-1); |
111 | 0 | } |
112 | | |
113 | 0 | handle->ci_discard_ilseq = strcasestr(out, "//IGNORE"); |
114 | 0 | handle->ci_ilseq_invalid = false; |
115 | 0 | handle->ci_hooks = NULL; |
116 | |
|
117 | 0 | return ((iconv_t)(void *)handle); |
118 | 0 | } |
119 | | |
120 | | iconv_t |
121 | | OFiconv_open(const char *out, const char *in) |
122 | 0 | { |
123 | |
|
124 | 0 | return (_iconv_open(out, in, NULL)); |
125 | 0 | } |
126 | | |
127 | | int |
128 | | OFiconv_open_into(const char *out, const char *in, iconv_allocation_t *ptr) |
129 | 0 | { |
130 | 0 | struct _citrus_iconv *handle; |
131 | |
|
132 | 0 | handle = (struct _citrus_iconv *)ptr; |
133 | 0 | return ((_iconv_open(out, in, handle) == (iconv_t)-1) ? -1 : 0); |
134 | 0 | } |
135 | | |
136 | | int |
137 | | OFiconv_close_in(iconv_allocation_t *ptr) |
138 | 0 | { |
139 | 0 | struct _citrus_iconv *handle; |
140 | |
|
141 | 0 | handle = (struct _citrus_iconv *)ptr; |
142 | |
|
143 | 0 | if (ISBADF((iconv_t)handle)) { |
144 | 0 | errno = EBADF; |
145 | 0 | return (-1); |
146 | 0 | } |
147 | | |
148 | 0 | _citrus_iconv_close_nofree(handle); |
149 | |
|
150 | 0 | return (0); |
151 | 0 | } |
152 | | |
153 | | int |
154 | | OFiconv_close(iconv_t handle) |
155 | 0 | { |
156 | 0 | if (ISBADF(handle)) { |
157 | 0 | errno = EBADF; |
158 | 0 | return (-1); |
159 | 0 | } |
160 | | |
161 | 0 | _citrus_iconv_close((struct _citrus_iconv *)(void *)handle); |
162 | |
|
163 | 0 | return (0); |
164 | 0 | } |
165 | | |
166 | | size_t |
167 | | OFiconv(iconv_t handle, char ** in, size_t * szin, char ** out, size_t * szout) |
168 | 0 | { |
169 | 0 | size_t ret; |
170 | 0 | int err; |
171 | |
|
172 | 0 | if (ISBADF(handle)) { |
173 | 0 | errno = EBADF; |
174 | 0 | return ((size_t)-1); |
175 | 0 | } |
176 | | |
177 | 0 | err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle, |
178 | 0 | in, szin, out, szout, 0, &ret); |
179 | 0 | if (err) { |
180 | 0 | errno = err; |
181 | 0 | ret = (size_t)-1; |
182 | 0 | } |
183 | |
|
184 | 0 | return (ret); |
185 | 0 | } |
186 | | |
187 | | size_t |
188 | | OF__iconv(iconv_t handle, char **in, size_t *szin, char **out, |
189 | | size_t *szout, uint32_t flags, size_t *invalids) |
190 | 0 | { |
191 | 0 | size_t ret; |
192 | 0 | int err; |
193 | |
|
194 | 0 | if (ISBADF(handle)) { |
195 | 0 | errno = EBADF; |
196 | 0 | return ((size_t)-1); |
197 | 0 | } |
198 | | |
199 | 0 | err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle, |
200 | 0 | in, szin, out, szout, flags, &ret); |
201 | 0 | if (invalids) |
202 | 0 | *invalids = ret; |
203 | 0 | if (err) { |
204 | 0 | errno = err; |
205 | 0 | ret = (size_t)-1; |
206 | 0 | } |
207 | |
|
208 | 0 | return (ret); |
209 | 0 | } |
210 | | |
211 | | int |
212 | | OF__iconv_get_list(char ***rlist, size_t *rsz, __iconv_bool sorted) |
213 | 0 | { |
214 | 0 | int ret; |
215 | |
|
216 | 0 | ret = _citrus_esdb_get_list(rlist, rsz, sorted); |
217 | 0 | if (ret) { |
218 | 0 | errno = ret; |
219 | 0 | return (-1); |
220 | 0 | } |
221 | | |
222 | 0 | return (0); |
223 | 0 | } |
224 | | |
225 | | void |
226 | | OF__iconv_free_list(char **list, size_t sz) |
227 | 0 | { |
228 | |
|
229 | 0 | _citrus_esdb_free_list(list, sz); |
230 | 0 | } |
231 | | |
232 | | /* |
233 | | * GNU-compatibile non-standard interfaces. |
234 | | */ |
235 | | static int |
236 | | qsort_helper(const void *first, const void *second) |
237 | 0 | { |
238 | 0 | const char * const *s1; |
239 | 0 | const char * const *s2; |
240 | |
|
241 | 0 | s1 = first; |
242 | 0 | s2 = second; |
243 | 0 | return (strcmp(*s1, *s2)); |
244 | 0 | } |
245 | | |
246 | | void |
247 | | OFiconvlist(int (*do_one) (unsigned int, const char * const *, |
248 | | void *), void *data) |
249 | 0 | { |
250 | 0 | char **list, **names; |
251 | 0 | const char * const *np; |
252 | 0 | char *curitem, *curkey, *slashpos; |
253 | 0 | size_t sz; |
254 | 0 | unsigned int i, j, n; |
255 | |
|
256 | 0 | i = 0; |
257 | 0 | j = 0; |
258 | 0 | names = NULL; |
259 | |
|
260 | 0 | if (OF__iconv_get_list(&list, &sz, true)) { |
261 | 0 | list = NULL; |
262 | 0 | goto out; |
263 | 0 | } |
264 | 0 | qsort((void *)list, sz, sizeof(char *), qsort_helper); |
265 | 0 | while (i < sz) { |
266 | 0 | j = 0; |
267 | 0 | slashpos = strchr(list[i], '/'); |
268 | 0 | names = malloc(sz * sizeof(char *)); |
269 | 0 | if (names == NULL) |
270 | 0 | goto out; |
271 | 0 | curkey = my_strndup(list[i], slashpos - list[i]); |
272 | 0 | if (curkey == NULL) |
273 | 0 | goto out; |
274 | 0 | names[j++] = curkey; |
275 | 0 | for (; (i < sz) && (memcmp(curkey, list[i], strlen(curkey)) == 0); i++) { |
276 | 0 | slashpos = strchr(list[i], '/'); |
277 | 0 | if (strcmp(curkey, &slashpos[1]) == 0) |
278 | 0 | continue; |
279 | 0 | curitem = strdup(&slashpos[1]); |
280 | 0 | if (curitem == NULL) |
281 | 0 | goto out; |
282 | 0 | names[j++] = curitem; |
283 | 0 | } |
284 | 0 | np = (const char * const *)names; |
285 | 0 | do_one(j, np, data); |
286 | 0 | for (n = 0; n < j; n++) |
287 | 0 | free(names[n]); |
288 | 0 | free(names); |
289 | 0 | names = NULL; |
290 | 0 | } |
291 | | |
292 | 0 | out: |
293 | 0 | if (names != NULL) { |
294 | 0 | for (n = 0; n < j; n++) |
295 | 0 | free(names[n]); |
296 | 0 | free(names); |
297 | 0 | } |
298 | 0 | if (list != NULL) |
299 | 0 | OF__iconv_free_list(list, sz); |
300 | 0 | } |
301 | | |
302 | | char *OFiconv_canonicalize(const char *name) |
303 | 0 | { |
304 | 0 | return (_citrus_iconv_canonicalize(name)); |
305 | 0 | } |
306 | | |
307 | | int |
308 | | OFiconvctl(iconv_t cd, int request, void *argument) |
309 | 0 | { |
310 | 0 | struct _citrus_iconv *cv; |
311 | 0 | struct iconv_hooks *hooks; |
312 | 0 | const char *convname; |
313 | 0 | char *dst; |
314 | 0 | int *i; |
315 | 0 | size_t srclen; |
316 | |
|
317 | 0 | cv = (struct _citrus_iconv *)(void *)cd; |
318 | 0 | hooks = (struct iconv_hooks *)argument; |
319 | 0 | i = (int *)argument; |
320 | |
|
321 | 0 | if (ISBADF(cd)) { |
322 | 0 | errno = EBADF; |
323 | 0 | return (-1); |
324 | 0 | } |
325 | | |
326 | 0 | switch (request) { |
327 | 0 | case ICONV_TRIVIALP: |
328 | 0 | convname = cv->cv_shared->ci_convname; |
329 | 0 | dst = strchr(convname, '/'); |
330 | 0 | srclen = dst - convname; |
331 | 0 | dst++; |
332 | 0 | *i = (srclen == strlen(dst)) && !memcmp(convname, dst, srclen); |
333 | 0 | return (0); |
334 | 0 | case ICONV_GET_TRANSLITERATE: |
335 | 0 | *i = 1; |
336 | 0 | return (0); |
337 | 0 | case ICONV_SET_TRANSLITERATE: |
338 | 0 | return ((*i == 1) ? 0 : -1); |
339 | 0 | case ICONV_GET_DISCARD_ILSEQ: |
340 | 0 | *i = cv->ci_discard_ilseq ? 1 : 0; |
341 | 0 | return (0); |
342 | 0 | case ICONV_SET_DISCARD_ILSEQ: |
343 | 0 | cv->ci_discard_ilseq = *i; |
344 | 0 | return (0); |
345 | 0 | case ICONV_SET_HOOKS: |
346 | 0 | cv->ci_hooks = hooks; |
347 | 0 | return (0); |
348 | 0 | case ICONV_SET_FALLBACKS: |
349 | 0 | errno = EOPNOTSUPP; |
350 | 0 | return (-1); |
351 | 0 | case ICONV_GET_ILSEQ_INVALID: |
352 | 0 | *i = cv->ci_ilseq_invalid ? 1 : 0; |
353 | 0 | return (0); |
354 | 0 | case ICONV_SET_ILSEQ_INVALID: |
355 | 0 | cv->ci_ilseq_invalid = *i; |
356 | 0 | return (0); |
357 | 0 | default: |
358 | 0 | errno = EINVAL; |
359 | 0 | return (-1); |
360 | 0 | } |
361 | 0 | } |
362 | | |
363 | | const char *OFlocale_charset(iconv_locale_allocation_t *buf) |
364 | 0 | { |
365 | | #ifdef HAVE_WINDOWS_H |
366 | | snprintf((char *)buf, sizeof(iconv_locale_allocation_t), "%lu", (unsigned long) GetConsoleOutputCP()); |
367 | | return (const char *)buf; |
368 | | #elif defined(__ANDROID__) |
369 | | /* Android does not provide an implementation of nl_langinfo() and internally always uses UTF-8 */ |
370 | | (void) buf; |
371 | | return "UTF-8"; |
372 | | #else |
373 | 0 | (void) buf; |
374 | 0 | return nl_langinfo(CODESET); |
375 | 0 | #endif |
376 | 0 | } |
377 | | |
378 | | void OFiconv_cleanup() |
379 | 0 | { |
380 | 0 | _citrus_csmapper_free(); |
381 | 0 | _citrus_cleanup_csmapper_none(); |
382 | | OFiconv_setpath(NULL); |
383 | 0 | } |