1 /* Transliteration using the locale's data.
2 Copyright (C) 2000, 2009 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, see
18 <http://www.gnu.org/licenses/>. */
27 #include <bits/libc-lock.h>
28 #include "gconv_int.h"
29 #include "../locale/localeinfo.h"
33 __gconv_transliterate (struct __gconv_step
*step
,
34 struct __gconv_step_data
*step_data
,
35 void *trans_data
__attribute__ ((unused
)),
36 const unsigned char *inbufstart
,
37 const unsigned char **inbufp
,
38 const unsigned char *inbufend
,
39 unsigned char **outbufstart
, size_t *irreversible
)
41 /* Find out about the locale's transliteration. */
43 const uint32_t *from_idx
;
44 const uint32_t *from_tbl
;
45 const uint32_t *to_idx
;
46 const uint32_t *to_tbl
;
47 const uint32_t *winbuf
;
48 const uint32_t *winbufend
;
52 /* The input buffer. There are actually 4-byte values. */
53 winbuf
= (const uint32_t *) *inbufp
;
54 winbufend
= (const uint32_t *) inbufend
;
56 __gconv_fct fct
= step
->__fct
;
58 if (step
->__shlib_handle
!= NULL
)
62 /* If there is no transliteration information in the locale don't do
63 anything and return the error. */
64 size
= _NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_TRANSLIT_TAB_SIZE
);
68 /* Get the rest of the values. */
70 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_FROM_IDX
);
72 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_FROM_TBL
);
74 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_TO_IDX
);
76 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_TO_TBL
);
78 /* Test whether there is enough input. */
79 if (winbuf
+ 1 > winbufend
)
80 return (winbuf
== winbufend
81 ? __GCONV_EMPTY_INPUT
: __GCONV_INCOMPLETE_INPUT
);
83 /* The array starting at FROM_IDX contains indeces to the string table
84 in FROM_TBL. The indeces are sorted wrt to the strings. I.e., we
85 are doing binary search. */
90 uint_fast32_t med
= (low
+ high
) / 2;
94 /* Compare the string at this index with the string at the current
95 position in the input buffer. */
100 if (from_tbl
[idx
+ cnt
] != winbuf
[cnt
])
101 /* Does not match. */
105 while (from_tbl
[idx
+ cnt
] != L
'\0' && winbuf
+ cnt
< winbufend
);
107 if (cnt
> 0 && from_tbl
[idx
+ cnt
] == L
'\0')
109 /* Found a matching input sequence. Now try to convert the
110 possible replacements. */
111 uint32_t idx2
= to_idx
[med
];
115 /* Determine length of replacement. */
116 uint_fast32_t len
= 0;
118 const unsigned char *toinptr
;
119 unsigned char *outptr
;
121 while (to_tbl
[idx2
+ len
] != L
'\0')
124 /* Try this input text. */
125 toinptr
= (const unsigned char *) &to_tbl
[idx2
];
126 outptr
= *outbufstart
;
127 res
= DL_CALL_FCT (fct
,
128 (step
, step_data
, &toinptr
,
129 (const unsigned char *) &to_tbl
[idx2
+ len
],
130 &outptr
, NULL
, 0, 0));
131 if (res
!= __GCONV_ILLEGAL_INPUT
)
133 /* If the conversion succeeds we have to increment the
135 if (res
== __GCONV_EMPTY_INPUT
)
137 *inbufp
+= cnt
* sizeof (uint32_t);
141 /* Do not increment the output pointer if we could not
142 store the entire output. */
143 if (res
!= __GCONV_FULL_OUTPUT
)
144 *outbufstart
= outptr
;
149 /* Next replacement. */
152 while (to_tbl
[idx2
] != L
'\0');
154 /* Nothing found, continue searching. */
157 /* This means that the input buffer contents matches a prefix of
158 an entry. Since we cannot match it unless we get more input,
159 we will tell the caller about it. */
160 return __GCONV_INCOMPLETE_INPUT
;
162 if (winbuf
+ cnt
>= winbufend
|| from_tbl
[idx
+ cnt
] < winbuf
[cnt
])
169 /* Maybe the character is supposed to be ignored. */
170 if (_NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_TRANSLIT_IGNORE_LEN
) != 0)
172 int n
= _NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_TRANSLIT_IGNORE_LEN
);
173 const uint32_t *ranges
=
174 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_IGNORE
);
175 const uint32_t wc
= *(const uint32_t *) (*inbufp
);
178 /* Test whether there is enough input. */
179 if (winbuf
+ 1 > winbufend
)
180 return (winbuf
== winbufend
181 ? __GCONV_EMPTY_INPUT
: __GCONV_INCOMPLETE_INPUT
);
183 for (i
= 0; i
< n
; ranges
+= 3, ++i
)
184 if (ranges
[0] <= wc
&& wc
<= ranges
[1]
185 && (wc
- ranges
[0]) % ranges
[2] == 0)
187 /* Matches the range. Ignore it. */
192 else if (wc
< ranges
[0])
193 /* There cannot be any other matching range since they are
198 /* One last chance: use the default replacement. */
199 if (_NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_TRANSLIT_DEFAULT_MISSING_LEN
) != 0)
201 const uint32_t *default_missing
= (const uint32_t *)
202 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_DEFAULT_MISSING
);
203 const unsigned char *toinptr
= (const unsigned char *) default_missing
;
204 uint32_t len
= _NL_CURRENT_WORD (LC_CTYPE
,
205 _NL_CTYPE_TRANSLIT_DEFAULT_MISSING_LEN
);
206 unsigned char *outptr
;
209 /* Test whether there is enough input. */
210 if (winbuf
+ 1 > winbufend
)
211 return (winbuf
== winbufend
212 ? __GCONV_EMPTY_INPUT
: __GCONV_INCOMPLETE_INPUT
);
214 outptr
= *outbufstart
;
215 res
= DL_CALL_FCT (fct
,
216 (step
, step_data
, &toinptr
,
217 (const unsigned char *) (default_missing
+ len
),
218 &outptr
, NULL
, 0, 0));
220 if (res
!= __GCONV_ILLEGAL_INPUT
)
222 /* If the conversion succeeds we have to increment the
224 if (res
== __GCONV_EMPTY_INPUT
)
226 /* This worked but is not reversible. */
231 *outbufstart
= outptr
;
237 /* Haven't found a match. */
238 return __GCONV_ILLEGAL_INPUT
;
242 /* Structure to represent results of found (or not) transliteration
246 /* This structure must remain the first member. */
247 struct trans_struct info
;
255 /* Tree with results of previous calls to __gconv_translit_find. */
256 static void *search_tree
;
258 /* We modify global data. */
259 __libc_lock_define_initialized (static, lock
);
262 /* Compare two transliteration entries. */
264 trans_compare (const void *p1
, const void *p2
)
266 const struct known_trans
*s1
= (const struct known_trans
*) p1
;
267 const struct known_trans
*s2
= (const struct known_trans
*) p2
;
269 return strcmp (s1
->info
.name
, s2
->info
.name
);
273 /* Open (maybe reopen) the module named in the struct. Get the function
274 and data structure pointers we need. */
276 open_translit (struct known_trans
*trans
)
278 __gconv_trans_query_fct queryfct
;
280 trans
->handle
= __libc_dlopen (trans
->fname
);
281 if (trans
->handle
== NULL
)
285 /* Find the required symbol. */
286 queryfct
= __libc_dlsym (trans
->handle
, "gconv_trans_context");
287 if (queryfct
== NULL
)
289 /* We cannot live with that. */
291 __libc_dlclose (trans
->handle
);
292 trans
->handle
= NULL
;
296 /* Get the context. */
297 if (queryfct (trans
->info
.name
, &trans
->info
.csnames
, &trans
->info
.ncsnames
)
301 /* Of course we also have to have the actual function. */
302 trans
->info
.trans_fct
= __libc_dlsym (trans
->handle
, "gconv_trans");
303 if (trans
->info
.trans_fct
== NULL
)
306 /* Now the optional functions. */
307 trans
->info
.trans_init_fct
=
308 __libc_dlsym (trans
->handle
, "gconv_trans_init");
309 trans
->info
.trans_context_fct
=
310 __libc_dlsym (trans
->handle
, "gconv_trans_context");
311 trans
->info
.trans_end_fct
=
312 __libc_dlsym (trans
->handle
, "gconv_trans_end");
314 trans
->open_count
= 1;
322 __gconv_translit_find (struct trans_struct
*trans
)
324 struct known_trans
**found
;
325 const struct path_elem
*runp
;
328 /* We have to have a name. */
329 assert (trans
->name
!= NULL
);
331 /* Acquire the lock. */
332 __libc_lock_lock (lock
);
334 /* See whether we know this module already. */
335 found
= __tfind (trans
, &search_tree
, trans_compare
);
338 /* Is this module available? */
339 if ((*found
)->handle
!= NULL
)
341 /* Maybe we have to reopen the file. */
342 if ((*found
)->handle
!= (void *) -1)
343 /* The object is not unloaded. */
345 else if (open_translit (*found
) == 0)
348 *trans
= (*found
)->info
;
349 (*found
)->open_count
++;
356 size_t name_len
= strlen (trans
->name
) + 1;
358 struct known_trans
*newp
;
360 /* We have to continue looking for the module. */
361 if (__gconv_path_elem
== NULL
)
364 /* See whether we have to append .so. */
365 if (name_len
<= 4 || memcmp (&trans
->name
[name_len
- 4], ".so", 3) != 0)
368 /* Create a new entry. */
369 newp
= (struct known_trans
*) malloc (sizeof (struct known_trans
)
370 + (__gconv_max_path_elem_len
377 /* Clear the struct. */
378 memset (newp
, '\0', sizeof (struct known_trans
));
380 /* Store a copy of the module name. */
381 newp
->info
.name
= cp
= (char *) (newp
+ 1);
382 cp
= __mempcpy (cp
, trans
->name
, name_len
);
386 /* Search in all the directories. */
387 for (runp
= __gconv_path_elem
; runp
->name
!= NULL
; ++runp
)
389 cp
= __mempcpy (__stpcpy ((char *) newp
->fname
, runp
->name
),
390 trans
->name
, name_len
);
392 memcpy (cp
, ".so", sizeof (".so"));
394 if (open_translit (newp
) == 0)
396 /* We found a module. */
405 /* In any case we'll add the entry to our search tree. */
406 if (__tsearch (newp
, &search_tree
, trans_compare
) == NULL
)
408 /* Yickes, this should not happen. Unload the object. */
410 /* XXX unload here. */
415 __libc_lock_unlock (lock
);