]> git.ipfire.org Git - thirdparty/glibc.git/blob - iconv/gconv_trans.c
__gconv_translit_find: Disable function [BZ #17187]
[thirdparty/glibc.git] / iconv / gconv_trans.c
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.
5
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.
10
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.
15
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/>. */
19
20 #include <assert.h>
21 #include <dlfcn.h>
22 #include <search.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <bits/libc-lock.h>
28 #include "gconv_int.h"
29 #include "../locale/localeinfo.h"
30
31
32 int
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)
40 {
41 /* Find out about the locale's transliteration. */
42 uint_fast32_t size;
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;
49 uint_fast32_t low;
50 uint_fast32_t high;
51
52 /* The input buffer. There are actually 4-byte values. */
53 winbuf = (const uint32_t *) *inbufp;
54 winbufend = (const uint32_t *) inbufend;
55
56 __gconv_fct fct = step->__fct;
57 #ifdef PTR_DEMANGLE
58 if (step->__shlib_handle != NULL)
59 PTR_DEMANGLE (fct);
60 #endif
61
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);
65 if (size == 0)
66 goto no_rules;
67
68 /* Get the rest of the values. */
69 from_idx =
70 (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_FROM_IDX);
71 from_tbl =
72 (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_FROM_TBL);
73 to_idx =
74 (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_TO_IDX);
75 to_tbl =
76 (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_TO_TBL);
77
78 /* Test whether there is enough input. */
79 if (winbuf + 1 > winbufend)
80 return (winbuf == winbufend
81 ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT);
82
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. */
86 low = 0;
87 high = size;
88 while (low < high)
89 {
90 uint_fast32_t med = (low + high) / 2;
91 uint32_t idx;
92 int cnt;
93
94 /* Compare the string at this index with the string at the current
95 position in the input buffer. */
96 idx = from_idx[med];
97 cnt = 0;
98 do
99 {
100 if (from_tbl[idx + cnt] != winbuf[cnt])
101 /* Does not match. */
102 break;
103 ++cnt;
104 }
105 while (from_tbl[idx + cnt] != L'\0' && winbuf + cnt < winbufend);
106
107 if (cnt > 0 && from_tbl[idx + cnt] == L'\0')
108 {
109 /* Found a matching input sequence. Now try to convert the
110 possible replacements. */
111 uint32_t idx2 = to_idx[med];
112
113 do
114 {
115 /* Determine length of replacement. */
116 uint_fast32_t len = 0;
117 int res;
118 const unsigned char *toinptr;
119 unsigned char *outptr;
120
121 while (to_tbl[idx2 + len] != L'\0')
122 ++len;
123
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)
132 {
133 /* If the conversion succeeds we have to increment the
134 input buffer. */
135 if (res == __GCONV_EMPTY_INPUT)
136 {
137 *inbufp += cnt * sizeof (uint32_t);
138 ++*irreversible;
139 res = __GCONV_OK;
140 }
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;
145
146 return res;
147 }
148
149 /* Next replacement. */
150 idx2 += len + 1;
151 }
152 while (to_tbl[idx2] != L'\0');
153
154 /* Nothing found, continue searching. */
155 }
156 else if (cnt > 0)
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;
161
162 if (winbuf + cnt >= winbufend || from_tbl[idx + cnt] < winbuf[cnt])
163 low = med + 1;
164 else
165 high = med;
166 }
167
168 no_rules:
169 /* Maybe the character is supposed to be ignored. */
170 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_IGNORE_LEN) != 0)
171 {
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);
176 int i;
177
178 /* Test whether there is enough input. */
179 if (winbuf + 1 > winbufend)
180 return (winbuf == winbufend
181 ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT);
182
183 for (i = 0; i < n; ranges += 3, ++i)
184 if (ranges[0] <= wc && wc <= ranges[1]
185 && (wc - ranges[0]) % ranges[2] == 0)
186 {
187 /* Matches the range. Ignore it. */
188 *inbufp += 4;
189 ++*irreversible;
190 return __GCONV_OK;
191 }
192 else if (wc < ranges[0])
193 /* There cannot be any other matching range since they are
194 sorted. */
195 break;
196 }
197
198 /* One last chance: use the default replacement. */
199 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_DEFAULT_MISSING_LEN) != 0)
200 {
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;
207 int res;
208
209 /* Test whether there is enough input. */
210 if (winbuf + 1 > winbufend)
211 return (winbuf == winbufend
212 ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT);
213
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));
219
220 if (res != __GCONV_ILLEGAL_INPUT)
221 {
222 /* If the conversion succeeds we have to increment the
223 input buffer. */
224 if (res == __GCONV_EMPTY_INPUT)
225 {
226 /* This worked but is not reversible. */
227 ++*irreversible;
228 *inbufp += 4;
229 res = __GCONV_OK;
230 }
231 *outbufstart = outptr;
232
233 return res;
234 }
235 }
236
237 /* Haven't found a match. */
238 return __GCONV_ILLEGAL_INPUT;
239 }
240
241 int
242 internal_function
243 __gconv_translit_find (struct trans_struct *trans)
244 {
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. */
248 return 1;
249 }