1 /* Transliteration using the locale's data.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <bits/libc-lock.h>
29 #include "gconv_int.h"
30 #include "../locale/localeinfo.h"
34 __gconv_transliterate (struct __gconv_step *step,
35 struct __gconv_step_data *step_data,
36 void *trans_data __attribute__ ((unused)),
37 const unsigned char *inbufstart,
38 const unsigned char **inbufp,
39 const unsigned char *inbufend,
40 unsigned char **outbufstart, size_t *irreversible)
42 /* Find out about the locale's transliteration. */
44 const uint32_t *from_idx;
45 const uint32_t *from_tbl;
46 const uint32_t *to_idx;
47 const uint32_t *to_tbl;
48 const uint32_t *winbuf;
49 const uint32_t *winbufend;
53 /* The input buffer. There are actually 4-byte values. */
54 winbuf = (const uint32_t *) *inbufp;
55 winbufend = (const uint32_t *) inbufend;
57 __gconv_fct fct = step->__fct;
59 if (step->__shlib_handle != NULL)
63 /* If there is no transliteration information in the locale don't do
64 anything and return the error. */
65 size = _NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_TAB_SIZE);
69 /* Get the rest of the values. */
71 (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_FROM_IDX);
73 (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_FROM_TBL);
75 (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_TO_IDX);
77 (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_TO_TBL);
79 /* Test whether there is enough input. */
80 if (winbuf + 1 > winbufend)
81 return (winbuf == winbufend
82 ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT);
84 /* The array starting at FROM_IDX contains indeces to the string table
85 in FROM_TBL. The indeces are sorted wrt to the strings. I.e., we
86 are doing binary search. */
91 uint_fast32_t med = (low + high) / 2;
95 /* Compare the string at this index with the string at the current
96 position in the input buffer. */
101 if (from_tbl[idx + cnt] != winbuf[cnt])
102 /* Does not match. */
106 while (from_tbl[idx + cnt] != L'\0' && winbuf + cnt < winbufend);
108 if (cnt > 0 && from_tbl[idx + cnt] == L'\0')
110 /* Found a matching input sequence. Now try to convert the
111 possible replacements. */
112 uint32_t idx2 = to_idx[med];
116 /* Determine length of replacement. */
117 uint_fast32_t len = 0;
119 const unsigned char *toinptr;
120 unsigned char *outptr;
122 while (to_tbl[idx2 + len] != L'\0')
125 /* Try this input text. */
126 toinptr = (const unsigned char *) &to_tbl[idx2];
127 outptr = *outbufstart;
128 res = DL_CALL_FCT (fct,
129 (step, step_data, &toinptr,
130 (const unsigned char *) &to_tbl[idx2 + len],
131 &outptr, NULL, 0, 0));
132 if (res != __GCONV_ILLEGAL_INPUT)
134 /* If the conversion succeeds we have to increment the
136 if (res == __GCONV_EMPTY_INPUT)
138 *inbufp += cnt * sizeof (uint32_t);
142 *outbufstart = outptr;
147 /* Next replacement. */
150 while (to_tbl[idx2] != L'\0');
152 /* Nothing found, continue searching. */
155 /* This means that the input buffer contents matches a prefix of
156 an entry. Since we cannot match it unless we get more input,
157 we will tell the caller about it. */
158 return __GCONV_INCOMPLETE_INPUT;
160 if (winbuf + cnt >= winbufend || from_tbl[idx + cnt] < winbuf[cnt])
167 /* Maybe the character is supposed to be ignored. */
168 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_IGNORE_LEN) != 0)
170 int n = _NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_IGNORE_LEN);
171 const uint32_t *ranges =
172 (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_IGNORE);
173 const uint32_t wc = *(const uint32_t *) (*inbufp);
176 /* Test whether there is enough input. */
177 if (winbuf + 1 > winbufend)
178 return (winbuf == winbufend
179 ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT);
181 for (i = 0; i < n; ranges += 3, ++i)
182 if (ranges[0] <= wc && wc <= ranges[1]
183 && (wc - ranges[0]) % ranges[2] == 0)
185 /* Matches the range. Ignore it. */
190 else if (wc < ranges[0])
191 /* There cannot be any other matching range since they are
196 /* One last chance: use the default replacement. */
197 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_DEFAULT_MISSING_LEN) != 0)
199 const uint32_t *default_missing = (const uint32_t *)
200 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_DEFAULT_MISSING);
201 const unsigned char *toinptr = (const unsigned char *) default_missing;
202 uint32_t len = _NL_CURRENT_WORD (LC_CTYPE,
203 _NL_CTYPE_TRANSLIT_DEFAULT_MISSING_LEN);
204 unsigned char *outptr;
207 /* Test whether there is enough input. */
208 if (winbuf + 1 > winbufend)
209 return (winbuf == winbufend
210 ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT);
212 outptr = *outbufstart;
213 res = DL_CALL_FCT (fct,
214 (step, step_data, &toinptr,
215 (const unsigned char *) (default_missing + len),
216 &outptr, NULL, 0, 0));
218 if (res != __GCONV_ILLEGAL_INPUT)
220 /* If the conversion succeeds we have to increment the
222 if (res == __GCONV_EMPTY_INPUT)
224 /* This worked but is not reversible. */
229 *outbufstart = outptr;
235 /* Haven't found a match. */
236 return __GCONV_ILLEGAL_INPUT;
240 /* Structure to represent results of found (or not) transliteration
244 /* This structure must remain the first member. */
245 struct trans_struct info;
253 /* Tree with results of previous calls to __gconv_translit_find. */
254 static void *search_tree;
256 /* We modify global data. */
257 __libc_lock_define_initialized (static, lock);
260 /* Compare two transliteration entries. */
262 trans_compare (const void *p1, const void *p2)
264 const struct known_trans *s1 = (const struct known_trans *) p1;
265 const struct known_trans *s2 = (const struct known_trans *) p2;
267 return strcmp (s1->info.name, s2->info.name);
271 /* Open (maybe reopen) the module named in the struct. Get the function
272 and data structure pointers we need. */
274 open_translit (struct known_trans *trans)
276 __gconv_trans_query_fct queryfct;
278 trans->handle = __libc_dlopen (trans->fname);
279 if (trans->handle == NULL)
283 /* Find the required symbol. */
284 queryfct = __libc_dlsym (trans->handle, "gconv_trans_context");
285 if (queryfct == NULL)
287 /* We cannot live with that. */
289 __libc_dlclose (trans->handle);
290 trans->handle = NULL;
294 /* Get the context. */
295 if (queryfct (trans->info.name, &trans->info.csnames, &trans->info.ncsnames)
299 /* Of course we also have to have the actual function. */
300 trans->info.trans_fct = __libc_dlsym (trans->handle, "gconv_trans");
301 if (trans->info.trans_fct == NULL)
304 /* Now the optional functions. */
305 trans->info.trans_init_fct =
306 __libc_dlsym (trans->handle, "gconv_trans_init");
307 trans->info.trans_context_fct =
308 __libc_dlsym (trans->handle, "gconv_trans_context");
309 trans->info.trans_end_fct =
310 __libc_dlsym (trans->handle, "gconv_trans_end");
312 trans->open_count = 1;
320 __gconv_translit_find (struct trans_struct *trans)
322 struct known_trans **found;
323 const struct path_elem *runp;
326 /* We have to have a name. */
327 assert (trans->name != NULL);
329 /* Acquire the lock. */
330 __libc_lock_lock (lock);
332 /* See whether we know this module already. */
333 found = __tfind (trans, &search_tree, trans_compare);
336 /* Is this module available? */
337 if ((*found)->handle != NULL)
339 /* Maybe we have to reopen the file. */
340 if ((*found)->handle != (void *) -1)
341 /* The object is not unloaded. */
343 else if (open_translit (*found) == 0)
346 *trans = (*found)->info;
347 (*found)->open_count++;
354 size_t name_len = strlen (trans->name) + 1;
356 struct known_trans *newp;
358 /* We have to continue looking for the module. */
359 if (__gconv_path_elem == NULL)
362 /* See whether we have to append .so. */
363 if (name_len <= 4 || memcmp (&trans->name[name_len - 4], ".so", 3) != 0)
366 /* Create a new entry. */
367 newp = (struct known_trans *) malloc (sizeof (struct known_trans)
368 + (__gconv_max_path_elem_len
375 /* Clear the struct. */
376 memset (newp, '\0', sizeof (struct known_trans));
378 /* Store a copy of the module name. */
379 newp->info.name = cp = (char *) (newp + 1);
380 cp = __mempcpy (cp, trans->name, name_len);
384 /* Search in all the directories. */
385 for (runp = __gconv_path_elem; runp->name != NULL; ++runp)
387 cp = __mempcpy (__stpcpy ((char *) newp->fname, runp->name),
388 trans->name, name_len);
390 memcpy (cp, ".so", sizeof (".so"));
392 if (open_translit (newp) == 0)
394 /* We found a module. */
403 /* In any case we'll add the entry to our search tree. */
404 if (__tsearch (newp, &search_tree, trans_compare) == NULL)
406 /* Yickes, this should not happen. Unload the object. */
408 /* XXX unload here. */
413 __libc_lock_unlock (lock);