1 /* Transliteration using the locale's data.
2 Copyright (C) 2000-2014 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
;
243 __gconv_translit_find (struct trans_struct
*trans
)
245 /* Transliteration module loading has been removed because it never
246 worked as intended and suffered from a security vulnerability.
247 Consequently, this function always fails. */