]>
git.ipfire.org Git - thirdparty/glibc.git/blob - iconv/gconv_open.c
1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997-2020 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 <https://www.gnu.org/licenses/>. */
22 #include "../locale/localeinfo.h"
26 #include <gconv_int.h>
29 /* How many character should be converted in one call? */
30 #define GCONV_NCHAR_GOAL 8160
34 __gconv_open (const char *toset
, const char *fromset
, __gconv_t
*handle
,
37 struct __gconv_step
*steps
;
39 __gconv_t result
= NULL
;
45 bool translit
= false;
47 /* Find out whether any error handling method is specified. */
48 errhand
= strchr (toset
, '/');
50 errhand
= strchr (errhand
+ 1, '/');
51 if (__glibc_likely (errhand
!= NULL
))
53 if (*++errhand
== '\0')
57 /* Make copy without the error handling description. */
58 char *newtoset
= (char *) alloca (errhand
- toset
+ 1);
60 char *ptr
= NULL
/* Work around a bogus warning */;
62 newtoset
[errhand
- toset
] = '\0';
63 toset
= memcpy (newtoset
, toset
, errhand
- toset
);
65 /* Find the appropriate transliteration handlers. */
66 tok
= strdupa (errhand
);
68 tok
= __strtok_r (tok
, ",", &ptr
);
71 if (__strcasecmp_l (tok
, "TRANSLIT", _nl_C_locobj_ptr
) == 0)
73 else if (__strcasecmp_l (tok
, "IGNORE", _nl_C_locobj_ptr
) == 0)
74 /* Set the flag to ignore all errors. */
75 conv_flags
|= __GCONV_IGNORE_ERRORS
;
77 tok
= __strtok_r (NULL
, ",", &ptr
);
82 /* For the source character set we ignore the error handler specification.
83 XXX Is this really always the best? */
84 ignore
= strchr (fromset
, '/');
85 if (ignore
!= NULL
&& (ignore
= strchr (ignore
+ 1, '/')) != NULL
88 char *newfromset
= (char *) alloca (ignore
- fromset
+ 1);
90 newfromset
[ignore
- fromset
] = '\0';
91 fromset
= memcpy (newfromset
, fromset
, ignore
- fromset
);
94 /* If the string is empty define this to mean the charset of the
95 currently selected locale. */
96 if (strcmp (toset
, "//") == 0)
98 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
99 size_t len
= strlen (codeset
);
101 toset
= dest
= (char *) alloca (len
+ 3);
102 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
104 if (strcmp (fromset
, "//") == 0)
106 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
107 size_t len
= strlen (codeset
);
109 fromset
= dest
= (char *) alloca (len
+ 3);
110 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
113 res
= __gconv_find_transform (toset
, fromset
, &steps
, &nsteps
, flags
);
114 if (res
== __GCONV_OK
)
116 /* Allocate room for handle. */
117 result
= (__gconv_t
) malloc (sizeof (struct __gconv_info
)
119 * sizeof (struct __gconv_step_data
)));
124 /* Remember the list of steps. */
125 result
->__steps
= steps
;
126 result
->__nsteps
= nsteps
;
128 /* Clear the array for the step data. */
129 memset (result
->__data
, '\0',
130 nsteps
* sizeof (struct __gconv_step_data
));
132 /* Call all initialization functions for the transformation
133 step implementations. */
134 for (cnt
= 0; cnt
< nsteps
; ++cnt
)
138 /* Would have to be done if we would not clear the whole
141 /* Reset the counter. */
142 result
->__data
[cnt
].__invocation_counter
= 0;
144 /* It's a regular use. */
145 result
->__data
[cnt
].__internal_use
= 0;
148 /* We use the `mbstate_t' member in DATA. */
149 result
->__data
[cnt
].__statep
= &result
->__data
[cnt
].__state
;
151 /* The builtin transliteration handling only
152 supports the internal encoding. */
154 && __strcasecmp_l (steps
[cnt
].__from_name
,
155 "INTERNAL", _nl_C_locobj_ptr
) == 0)
156 conv_flags
|= __GCONV_TRANSLIT
;
158 /* If this is the last step we must not allocate an
160 if (cnt
< nsteps
- 1)
162 result
->__data
[cnt
].__flags
= conv_flags
;
164 /* Allocate the buffer. */
165 size
= (GCONV_NCHAR_GOAL
* steps
[cnt
].__max_needed_to
);
167 result
->__data
[cnt
].__outbuf
= malloc (size
);
168 if (result
->__data
[cnt
].__outbuf
== NULL
)
174 result
->__data
[cnt
].__outbufend
=
175 result
->__data
[cnt
].__outbuf
+ size
;
179 /* Handle the last entry. */
180 result
->__data
[cnt
].__flags
= conv_flags
| __GCONV_IS_LAST
;
187 if (res
!= __GCONV_OK
)
189 /* Something went wrong. Free all the resources. */
197 free (result
->__data
[cnt
].__outbuf
);
203 __gconv_close_transform (steps
, nsteps
);
205 __set_errno (serrno
);