1 /* Transliteration using the locale's data.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
26 #include <libc-lock.h>
27 #include "gconv_int.h"
28 #include "../locale/localeinfo.h"
32 __gconv_transliterate (struct __gconv_step
*step
,
33 struct __gconv_step_data
*step_data
,
34 const unsigned char *inbufstart
,
35 const unsigned char **inbufp
,
36 const unsigned char *inbufend
,
37 unsigned char **outbufstart
, size_t *irreversible
)
39 /* Find out about the locale's transliteration. */
41 const uint32_t *from_idx
;
42 const uint32_t *from_tbl
;
43 const uint32_t *to_idx
;
44 const uint32_t *to_tbl
;
45 const uint32_t *winbuf
;
46 const uint32_t *winbufend
;
50 /* The input buffer. There are actually 4-byte values. */
51 winbuf
= (const uint32_t *) *inbufp
;
52 winbufend
= (const uint32_t *) inbufend
;
54 __gconv_fct fct
= step
->__fct
;
56 if (step
->__shlib_handle
!= NULL
)
60 /* If there is no transliteration information in the locale don't do
61 anything and return the error. */
62 size
= _NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_TRANSLIT_TAB_SIZE
);
66 /* Get the rest of the values. */
68 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_FROM_IDX
);
70 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_FROM_TBL
);
72 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_TO_IDX
);
74 (const uint32_t *) _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_TRANSLIT_TO_TBL
);
76 /* Test whether there is enough input. */
77 if (winbuf
+ 1 > winbufend
)
78 return (winbuf
== winbufend
79 ? __GCONV_EMPTY_INPUT
: __GCONV_INCOMPLETE_INPUT
);
81 /* The array starting at FROM_IDX contains indices to the string table
82 in FROM_TBL. The indices are sorted wrt to the strings. I.e., we
83 are doing binary search. */
88 uint_fast32_t med
= (low
+ high
) / 2;
92 /* Compare the string at this index with the string at the current
93 position in the input buffer. */
98 if (from_tbl
[idx
+ cnt
] != winbuf
[cnt
])
103 while (from_tbl
[idx
+ cnt
] != L
'\0' && winbuf
+ cnt
< winbufend
);
105 if (cnt
> 0 && from_tbl
[idx
+ cnt
] == L
'\0')
107 /* Found a matching input sequence. Now try to convert the
108 possible replacements. */
109 uint32_t idx2
= to_idx
[med
];
113 /* Determine length of replacement. */
114 uint_fast32_t len
= 0;
116 const unsigned char *toinptr
;
117 unsigned char *outptr
;
119 while (to_tbl
[idx2
+ len
] != L
'\0')
122 /* Try this input text. */
123 toinptr
= (const unsigned char *) &to_tbl
[idx2
];
124 outptr
= *outbufstart
;
125 res
= DL_CALL_FCT (fct
,
126 (step
, step_data
, &toinptr
,
127 (const unsigned char *) &to_tbl
[idx2
+ len
],
128 &outptr
, NULL
, 0, 0));
129 if (res
!= __GCONV_ILLEGAL_INPUT
)
131 /* If the conversion succeeds we have to increment the
133 if (res
== __GCONV_EMPTY_INPUT
)
135 *inbufp
+= cnt
* sizeof (uint32_t);
139 /* Do not increment the output pointer if we could not
140 store the entire output. */
141 if (res
!= __GCONV_FULL_OUTPUT
)
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
;
238 libc_hidden_def (__gconv_transliterate
)