Simplify strncat.
[glibc.git] / iconv / gconv_open.c
blob615f33dfd2e8de912e54f65f571829e3c82c00e0
1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997-2014 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/>. */
20 #include <errno.h>
21 #include <locale.h>
22 #include "../locale/localeinfo.h"
23 #include <stdlib.h>
24 #include <string.h>
26 #include <gconv_int.h>
29 int
30 internal_function
31 __gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
32 int flags)
34 struct __gconv_step *steps;
35 size_t nsteps;
36 __gconv_t result = NULL;
37 size_t cnt = 0;
38 int res;
39 int conv_flags = 0;
40 const char *errhand;
41 const char *ignore;
42 bool translit = false;
44 /* Find out whether any error handling method is specified. */
45 errhand = strchr (toset, '/');
46 if (errhand != NULL)
47 errhand = strchr (errhand + 1, '/');
48 if (__glibc_likely (errhand != NULL))
50 if (*++errhand == '\0')
51 errhand = NULL;
52 else
54 /* Make copy without the error handling description. */
55 char *newtoset = (char *) alloca (errhand - toset + 1);
56 char *tok;
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);
66 while (tok != NULL)
68 if (__strcasecmp_l (tok, "TRANSLIT", _nl_C_locobj_ptr) == 0)
69 translit = true;
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
83 && *++ignore != '\0')
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);
97 char *dest;
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);
105 char *dest;
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)
115 + (nsteps
116 * sizeof (struct __gconv_step_data)));
117 if (result == NULL)
118 res = __GCONV_NOMEM;
119 else
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)
133 size_t size;
135 /* Would have to be done if we would not clear the whole
136 array above. */
137 #if 0
138 /* Reset the counter. */
139 result->__data[cnt].__invocation_counter = 0;
141 /* It's a regular use. */
142 result->__data[cnt].__internal_use = 0;
143 #endif
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. */
150 if (translit
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
156 output buffer. */
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)
167 res = __GCONV_NOMEM;
168 goto bail;
171 result->__data[cnt].__outbufend =
172 result->__data[cnt].__outbuf + size;
174 else
176 /* Handle the last entry. */
177 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
179 break;
184 if (res != __GCONV_OK)
186 /* Something went wrong. Free all the resources. */
187 int serrno;
188 bail:
189 serrno = errno;
191 if (result != NULL)
193 while (cnt-- > 0)
194 free (result->__data[cnt].__outbuf);
196 free (result);
197 result = NULL;
200 __gconv_close_transform (steps, nsteps);
202 __set_errno (serrno);
206 *handle = result;
207 return res;