1 /* Transliteration using the locale's data.
2 Copyright (C) 2000-2017 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 <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 const unsigned char *inbufstart
,
36 const unsigned char **inbufp
,
37 const unsigned char *inbufend
,
38 unsigned char **outbufstart
, size_t *irreversible
)
40 /* Find out about the locale's transliteration. */
42 const uint32_t *from_idx
;
43 const uint32_t *from_tbl
;
44 const uint32_t *to_idx
;
45 const uint32_t *to_tbl
;
46 const uint32_t *winbuf
;
47 const uint32_t *winbufend
;
51 /* The input buffer. There are actually 4-byte values. */
52 winbuf
= (const uint32_t *) *inbufp
;
53 winbufend
= (const uint32_t *) inbufend
;
55 __gconv_fct fct
= step
->__fct
;
57 if (step
->__shlib_handle
!= NULL
)
61 /* If there is no transliteration information in the locale don't do
62 anything and return the error. */
63 size
= _NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_TRANSLIT_TAB_SIZE
);
67 /* Get the rest of the values. */
69 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_FROM_IDX
);
71 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_FROM_TBL
);
73 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_TO_IDX
);
75 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_TO_TBL
);
77 /* Test whether there is enough input. */
78 if (winbuf
+ 1 > winbufend
)
79 return (winbuf
== winbufend
80 ? __GCONV_EMPTY_INPUT
: __GCONV_INCOMPLETE_INPUT
);
82 /* The array starting at FROM_IDX contains indeces to the string table
83 in FROM_TBL. The indeces are sorted wrt to the strings. I.e., we
84 are doing binary search. */
89 uint_fast32_t med
= (low
+ high
) / 2;
93 /* Compare the string at this index with the string at the current
94 position in the input buffer. */
99 if (from_tbl
[idx
+ cnt
] != winbuf
[cnt
])
100 /* Does not match. */
104 while (from_tbl
[idx
+ cnt
] != L
'\0' && winbuf
+ cnt
< winbufend
);
106 if (cnt
> 0 && from_tbl
[idx
+ cnt
] == L
'\0')
108 /* Found a matching input sequence. Now try to convert the
109 possible replacements. */
110 uint32_t idx2
= to_idx
[med
];
114 /* Determine length of replacement. */
115 uint_fast32_t len
= 0;
117 const unsigned char *toinptr
;
118 unsigned char *outptr
;
120 while (to_tbl
[idx2
+ len
] != L
'\0')
123 /* Try this input text. */
124 toinptr
= (const unsigned char *) &to_tbl
[idx2
];
125 outptr
= *outbufstart
;
126 res
= DL_CALL_FCT (fct
,
127 (step
, step_data
, &toinptr
,
128 (const unsigned char *) &to_tbl
[idx2
+ len
],
129 &outptr
, NULL
, 0, 0));
130 if (res
!= __GCONV_ILLEGAL_INPUT
)
132 /* If the conversion succeeds we have to increment the
134 if (res
== __GCONV_EMPTY_INPUT
)
136 *inbufp
+= cnt
* sizeof (uint32_t);
140 /* Do not increment the output pointer if we could not
141 store the entire output. */
142 if (res
!= __GCONV_FULL_OUTPUT
)
143 *outbufstart
= outptr
;
148 /* Next replacement. */
151 while (to_tbl
[idx2
] != L
'\0');
153 /* Nothing found, continue searching. */
156 /* This means that the input buffer contents matches a prefix of
157 an entry. Since we cannot match it unless we get more input,
158 we will tell the caller about it. */
159 return __GCONV_INCOMPLETE_INPUT
;
161 if (winbuf
+ cnt
>= winbufend
|| from_tbl
[idx
+ cnt
] < winbuf
[cnt
])
168 /* Maybe the character is supposed to be ignored. */
169 if (_NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_TRANSLIT_IGNORE_LEN
) != 0)
171 int n
= _NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_TRANSLIT_IGNORE_LEN
);
172 const uint32_t *ranges
=
173 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_IGNORE
);
174 const uint32_t wc
= *(const uint32_t *) (*inbufp
);
177 /* Test whether there is enough input. */
178 if (winbuf
+ 1 > winbufend
)
179 return (winbuf
== winbufend
180 ? __GCONV_EMPTY_INPUT
: __GCONV_INCOMPLETE_INPUT
);
182 for (i
= 0; i
< n
; ranges
+= 3, ++i
)
183 if (ranges
[0] <= wc
&& wc
<= ranges
[1]
184 && (wc
- ranges
[0]) % ranges
[2] == 0)
186 /* Matches the range. Ignore it. */
191 else if (wc
< ranges
[0])
192 /* There cannot be any other matching range since they are
197 /* One last chance: use the default replacement. */
198 if (_NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_TRANSLIT_DEFAULT_MISSING_LEN
) != 0)
200 const uint32_t *default_missing
= (const uint32_t *)
201 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_DEFAULT_MISSING
);
202 const unsigned char *toinptr
= (const unsigned char *) default_missing
;
203 uint32_t len
= _NL_CURRENT_WORD (LC_CTYPE
,
204 _NL_CTYPE_TRANSLIT_DEFAULT_MISSING_LEN
);
205 unsigned char *outptr
;
208 /* Test whether there is enough input. */
209 if (winbuf
+ 1 > winbufend
)
210 return (winbuf
== winbufend
211 ? __GCONV_EMPTY_INPUT
: __GCONV_INCOMPLETE_INPUT
);
213 outptr
= *outbufstart
;
214 res
= DL_CALL_FCT (fct
,
215 (step
, step_data
, &toinptr
,
216 (const unsigned char *) (default_missing
+ len
),
217 &outptr
, NULL
, 0, 0));
219 if (res
!= __GCONV_ILLEGAL_INPUT
)
221 /* If the conversion succeeds we have to increment the
223 if (res
== __GCONV_EMPTY_INPUT
)
225 /* This worked but is not reversible. */
230 *outbufstart
= outptr
;
236 /* Haven't found a match. */
237 return __GCONV_ILLEGAL_INPUT
;
239 libc_hidden_def (__gconv_transliterate
)