1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997-2015 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>
31 __gconv_open (const char *toset
, const char *fromset
, __gconv_t
*handle
,
34 struct __gconv_step
*steps
;
36 __gconv_t result
= NULL
;
42 bool translit
= false;
44 /* Find out whether any error handling method is specified. */
45 errhand
= strchr (toset
, '/');
47 errhand
= strchr (errhand
+ 1, '/');
48 if (__glibc_likely (errhand
!= NULL
))
50 if (*++errhand
== '\0')
54 /* Make copy without the error handling description. */
55 char *newtoset
= (char *) alloca (errhand
- toset
+ 1);
57 char *ptr
= NULL
/* Work around a bogus warning */;
59 newtoset
[errhand
- toset
] = '\0';
60 toset
= memcpy (newtoset
, toset
, errhand
- toset
);
62 /* Find the appropriate transliteration handlers. */
63 tok
= strdupa (errhand
);
65 tok
= __strtok_r (tok
, ",", &ptr
);
68 if (__strcasecmp_l (tok
, "TRANSLIT", _nl_C_locobj_ptr
) == 0)
70 else if (__strcasecmp_l (tok
, "IGNORE", _nl_C_locobj_ptr
) == 0)
71 /* Set the flag to ignore all errors. */
72 conv_flags
|= __GCONV_IGNORE_ERRORS
;
74 tok
= __strtok_r (NULL
, ",", &ptr
);
79 /* For the source character set we ignore the error handler specification.
80 XXX Is this really always the best? */
81 ignore
= strchr (fromset
, '/');
82 if (ignore
!= NULL
&& (ignore
= strchr (ignore
+ 1, '/')) != NULL
85 char *newfromset
= (char *) alloca (ignore
- fromset
+ 1);
87 newfromset
[ignore
- fromset
] = '\0';
88 fromset
= memcpy (newfromset
, fromset
, ignore
- fromset
);
91 /* If the string is empty define this to mean the charset of the
92 currently selected locale. */
93 if (strcmp (toset
, "//") == 0)
95 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
96 size_t len
= strlen (codeset
);
98 toset
= dest
= (char *) alloca (len
+ 3);
99 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
101 if (strcmp (fromset
, "//") == 0)
103 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
104 size_t len
= strlen (codeset
);
106 fromset
= dest
= (char *) alloca (len
+ 3);
107 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
110 res
= __gconv_find_transform (toset
, fromset
, &steps
, &nsteps
, flags
);
111 if (res
== __GCONV_OK
)
113 /* Allocate room for handle. */
114 result
= (__gconv_t
) malloc (sizeof (struct __gconv_info
)
116 * sizeof (struct __gconv_step_data
)));
121 /* Remember the list of steps. */
122 result
->__steps
= steps
;
123 result
->__nsteps
= nsteps
;
125 /* Clear the array for the step data. */
126 memset (result
->__data
, '\0',
127 nsteps
* sizeof (struct __gconv_step_data
));
129 /* Call all initialization functions for the transformation
130 step implementations. */
131 for (cnt
= 0; cnt
< nsteps
; ++cnt
)
135 /* Would have to be done if we would not clear the whole
138 /* Reset the counter. */
139 result
->__data
[cnt
].__invocation_counter
= 0;
141 /* It's a regular use. */
142 result
->__data
[cnt
].__internal_use
= 0;
145 /* We use the `mbstate_t' member in DATA. */
146 result
->__data
[cnt
].__statep
= &result
->__data
[cnt
].__state
;
148 /* The builtin transliteration handling only
149 supports the internal encoding. */
151 && __strcasecmp_l (steps
[cnt
].__from_name
,
152 "INTERNAL", _nl_C_locobj_ptr
) == 0)
153 conv_flags
|= __GCONV_TRANSLIT
;
155 /* If this is the last step we must not allocate an
157 if (cnt
< nsteps
- 1)
159 result
->__data
[cnt
].__flags
= conv_flags
;
161 /* Allocate the buffer. */
162 size
= (GCONV_NCHAR_GOAL
* steps
[cnt
].__max_needed_to
);
164 result
->__data
[cnt
].__outbuf
= malloc (size
);
165 if (result
->__data
[cnt
].__outbuf
== NULL
)
171 result
->__data
[cnt
].__outbufend
=
172 result
->__data
[cnt
].__outbuf
+ size
;
176 /* Handle the last entry. */
177 result
->__data
[cnt
].__flags
= conv_flags
| __GCONV_IS_LAST
;
184 if (res
!= __GCONV_OK
)
186 /* Something went wrong. Free all the resources. */
194 free (result
->__data
[cnt
].__outbuf
);
200 __gconv_close_transform (steps
, nsteps
);
202 __set_errno (serrno
);