]>
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 (struct gconv_spec
*conv_spec
, __gconv_t
*handle
,
37 struct __gconv_step
*steps
;
39 __gconv_t result
= NULL
;
43 bool translit
= false;
44 char *tocode
, *fromcode
;
46 /* Find out whether any error handling method is specified. */
47 translit
= conv_spec
->translit
;
49 if (conv_spec
->ignore
)
50 conv_flags
|= __GCONV_IGNORE_ERRORS
;
52 tocode
= conv_spec
->tocode
;
53 fromcode
= conv_spec
->fromcode
;
55 /* If the string is empty define this to mean the charset of the
56 currently selected locale. */
57 if (strcmp (tocode
, "//") == 0)
59 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
60 size_t len
= strlen (codeset
);
62 tocode
= dest
= (char *) alloca (len
+ 3);
63 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
65 if (strcmp (fromcode
, "//") == 0)
67 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
68 size_t len
= strlen (codeset
);
70 fromcode
= dest
= (char *) alloca (len
+ 3);
71 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
74 res
= __gconv_find_transform (tocode
, fromcode
, &steps
, &nsteps
, flags
);
75 if (res
== __GCONV_OK
)
77 /* Allocate room for handle. */
78 result
= (__gconv_t
) malloc (sizeof (struct __gconv_info
)
80 * sizeof (struct __gconv_step_data
)));
85 /* Remember the list of steps. */
86 result
->__steps
= steps
;
87 result
->__nsteps
= nsteps
;
89 /* Clear the array for the step data. */
90 memset (result
->__data
, '\0',
91 nsteps
* sizeof (struct __gconv_step_data
));
93 /* Call all initialization functions for the transformation
94 step implementations. */
95 for (cnt
= 0; cnt
< nsteps
; ++cnt
)
99 /* Would have to be done if we would not clear the whole
102 /* Reset the counter. */
103 result
->__data
[cnt
].__invocation_counter
= 0;
105 /* It's a regular use. */
106 result
->__data
[cnt
].__internal_use
= 0;
109 /* We use the `mbstate_t' member in DATA. */
110 result
->__data
[cnt
].__statep
= &result
->__data
[cnt
].__state
;
112 /* The builtin transliteration handling only
113 supports the internal encoding. */
115 && __strcasecmp_l (steps
[cnt
].__from_name
,
116 "INTERNAL", _nl_C_locobj_ptr
) == 0)
117 conv_flags
|= __GCONV_TRANSLIT
;
119 /* If this is the last step we must not allocate an
121 if (cnt
< nsteps
- 1)
123 result
->__data
[cnt
].__flags
= conv_flags
;
125 /* Allocate the buffer. */
126 size
= (GCONV_NCHAR_GOAL
* steps
[cnt
].__max_needed_to
);
128 result
->__data
[cnt
].__outbuf
= malloc (size
);
129 if (result
->__data
[cnt
].__outbuf
== NULL
)
135 result
->__data
[cnt
].__outbufend
=
136 result
->__data
[cnt
].__outbuf
+ size
;
140 /* Handle the last entry. */
141 result
->__data
[cnt
].__flags
= conv_flags
| __GCONV_IS_LAST
;
148 if (res
!= __GCONV_OK
)
150 /* Something went wrong. Free all the resources. */
158 free (result
->__data
[cnt
].__outbuf
);
164 __gconv_close_transform (steps
, nsteps
);
166 __set_errno (serrno
);
173 libc_hidden_def (__gconv_open
)