1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997-2023 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/>. */
21 #include "../locale/localeinfo.h"
25 #include <gconv_int.h>
28 /* How many character should be converted in one call? */
29 #define GCONV_NCHAR_GOAL 8160
33 __gconv_open (struct gconv_spec
*conv_spec
, __gconv_t
*handle
,
36 struct __gconv_step
*steps
;
38 __gconv_t result
= NULL
;
42 bool translit
= false;
43 char *tocode
, *fromcode
;
45 /* Find out whether any error handling method is specified. */
46 translit
= conv_spec
->translit
;
48 if (conv_spec
->ignore
)
49 conv_flags
|= __GCONV_IGNORE_ERRORS
;
51 tocode
= conv_spec
->tocode
;
52 fromcode
= conv_spec
->fromcode
;
54 /* If the string is empty define this to mean the charset of the
55 currently selected locale. */
56 if (strcmp (tocode
, "//") == 0)
58 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
59 size_t len
= strlen (codeset
);
61 tocode
= dest
= (char *) alloca (len
+ 3);
62 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
64 if (strcmp (fromcode
, "//") == 0)
66 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
67 size_t len
= strlen (codeset
);
69 fromcode
= dest
= (char *) alloca (len
+ 3);
70 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
73 res
= __gconv_find_transform (tocode
, fromcode
, &steps
, &nsteps
, flags
);
74 if (res
== __GCONV_OK
)
76 /* Allocate room for handle. */
77 result
= (__gconv_t
) malloc (sizeof (struct __gconv_info
)
79 * sizeof (struct __gconv_step_data
)));
84 /* Remember the list of steps. */
85 result
->__steps
= steps
;
86 result
->__nsteps
= nsteps
;
88 /* Clear the array for the step data. */
89 memset (result
->__data
, '\0',
90 nsteps
* sizeof (struct __gconv_step_data
));
92 /* Call all initialization functions for the transformation
93 step implementations. */
94 for (cnt
= 0; cnt
< nsteps
; ++cnt
)
98 /* Would have to be done if we would not clear the whole
101 /* Reset the counter. */
102 result
->__data
[cnt
].__invocation_counter
= 0;
104 /* It's a regular use. */
105 result
->__data
[cnt
].__internal_use
= 0;
108 /* We use the `mbstate_t' member in DATA. */
109 result
->__data
[cnt
].__statep
= &result
->__data
[cnt
].__state
;
111 /* The builtin transliteration handling only
112 supports the internal encoding. */
114 && __strcasecmp_l (steps
[cnt
].__from_name
,
115 "INTERNAL", _nl_C_locobj_ptr
) == 0)
116 conv_flags
|= __GCONV_TRANSLIT
;
118 /* If this is the last step we must not allocate an
120 if (cnt
< nsteps
- 1)
122 result
->__data
[cnt
].__flags
= conv_flags
;
124 /* Allocate the buffer. */
125 size
= (GCONV_NCHAR_GOAL
* steps
[cnt
].__max_needed_to
);
127 result
->__data
[cnt
].__outbuf
= malloc (size
);
128 if (result
->__data
[cnt
].__outbuf
== NULL
)
134 result
->__data
[cnt
].__outbufend
=
135 result
->__data
[cnt
].__outbuf
+ size
;
139 /* Handle the last entry. */
140 result
->__data
[cnt
].__flags
= conv_flags
| __GCONV_IS_LAST
;
147 if (res
!= __GCONV_OK
)
149 /* Something went wrong. Free all the resources. */
157 free (result
->__data
[cnt
].__outbuf
);
163 __gconv_close_transform (steps
, nsteps
);
165 __set_errno (serrno
);
172 libc_hidden_def (__gconv_open
)