1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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/>. */
22 #include "../locale/localeinfo.h"
26 #include <gconv_int.h>
30 __gconv_open (const char *toset
, const char *fromset
, __gconv_t
*handle
,
33 struct __gconv_step
*steps
;
35 __gconv_t result
= NULL
;
41 bool translit
= false;
43 /* Find out whether any error handling method is specified. */
44 errhand
= strchr (toset
, '/');
46 errhand
= strchr (errhand
+ 1, '/');
47 if (__glibc_likely (errhand
!= NULL
))
49 if (*++errhand
== '\0')
53 /* Make copy without the error handling description. */
54 char *newtoset
= (char *) alloca (errhand
- toset
+ 1);
56 char *ptr
= NULL
/* Work around a bogus warning */;
58 newtoset
[errhand
- toset
] = '\0';
59 toset
= memcpy (newtoset
, toset
, errhand
- toset
);
61 /* Find the appropriate transliteration handlers. */
62 tok
= strdupa (errhand
);
64 tok
= __strtok_r (tok
, ",", &ptr
);
67 if (__strcasecmp_l (tok
, "TRANSLIT", _nl_C_locobj_ptr
) == 0)
69 else if (__strcasecmp_l (tok
, "IGNORE", _nl_C_locobj_ptr
) == 0)
70 /* Set the flag to ignore all errors. */
71 conv_flags
|= __GCONV_IGNORE_ERRORS
;
73 tok
= __strtok_r (NULL
, ",", &ptr
);
78 /* For the source character set we ignore the error handler specification.
79 XXX Is this really always the best? */
80 ignore
= strchr (fromset
, '/');
81 if (ignore
!= NULL
&& (ignore
= strchr (ignore
+ 1, '/')) != NULL
84 char *newfromset
= (char *) alloca (ignore
- fromset
+ 1);
86 newfromset
[ignore
- fromset
] = '\0';
87 fromset
= memcpy (newfromset
, fromset
, ignore
- fromset
);
90 /* If the string is empty define this to mean the charset of the
91 currently selected locale. */
92 if (strcmp (toset
, "//") == 0)
94 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
95 size_t len
= strlen (codeset
);
97 toset
= dest
= (char *) alloca (len
+ 3);
98 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
100 if (strcmp (fromset
, "//") == 0)
102 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
103 size_t len
= strlen (codeset
);
105 fromset
= dest
= (char *) alloca (len
+ 3);
106 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
109 res
= __gconv_find_transform (toset
, fromset
, &steps
, &nsteps
, flags
);
110 if (res
== __GCONV_OK
)
112 /* Allocate room for handle. */
113 result
= (__gconv_t
) malloc (sizeof (struct __gconv_info
)
115 * sizeof (struct __gconv_step_data
)));
120 /* Remember the list of steps. */
121 result
->__steps
= steps
;
122 result
->__nsteps
= nsteps
;
124 /* Clear the array for the step data. */
125 memset (result
->__data
, '\0',
126 nsteps
* sizeof (struct __gconv_step_data
));
128 /* Call all initialization functions for the transformation
129 step implementations. */
130 for (cnt
= 0; cnt
< nsteps
; ++cnt
)
134 /* Would have to be done if we would not clear the whole
137 /* Reset the counter. */
138 result
->__data
[cnt
].__invocation_counter
= 0;
140 /* It's a regular use. */
141 result
->__data
[cnt
].__internal_use
= 0;
144 /* We use the `mbstate_t' member in DATA. */
145 result
->__data
[cnt
].__statep
= &result
->__data
[cnt
].__state
;
147 /* The builtin transliteration handling only
148 supports the internal encoding. */
150 && __strcasecmp_l (steps
[cnt
].__from_name
,
151 "INTERNAL", _nl_C_locobj_ptr
) == 0)
152 conv_flags
|= __GCONV_TRANSLIT
;
154 /* If this is the last step we must not allocate an
156 if (cnt
< nsteps
- 1)
158 result
->__data
[cnt
].__flags
= conv_flags
;
160 /* Allocate the buffer. */
161 size
= (GCONV_NCHAR_GOAL
* steps
[cnt
].__max_needed_to
);
163 result
->__data
[cnt
].__outbuf
= malloc (size
);
164 if (result
->__data
[cnt
].__outbuf
== NULL
)
170 result
->__data
[cnt
].__outbufend
=
171 result
->__data
[cnt
].__outbuf
+ size
;
175 /* Handle the last entry. */
176 result
->__data
[cnt
].__flags
= conv_flags
| __GCONV_IS_LAST
;
183 if (res
!= __GCONV_OK
)
185 /* Something went wrong. Free all the resources. */
193 free (result
->__data
[cnt
].__outbuf
);
199 __gconv_close_transform (steps
, nsteps
);
201 __set_errno (serrno
);