]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/gconv_open.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / iconv / gconv_open.c
CommitLineData
6973fc01 1/* Find matching transformation algorithms and initialize steps.
bfff8b1b 2 Copyright (C) 1997-2017 Free Software Foundation, Inc.
6973fc01
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
6973fc01
UD
10
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
41bdb6e2 14 Lesser General Public License for more details.
6973fc01 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
6973fc01
UD
19
20#include <errno.h>
5db91571 21#include <locale.h>
e0e86ccb 22#include "../locale/localeinfo.h"
6973fc01 23#include <stdlib.h>
fd19ed3d 24#include <string.h>
6973fc01 25
e62c19f1
UD
26#include <gconv_int.h>
27
6973fc01
UD
28
29int
e62c19f1 30internal_function
c90a2db6
UD
31__gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
32 int flags)
6973fc01 33{
d64b6ad0 34 struct __gconv_step *steps;
6973fc01 35 size_t nsteps;
d64b6ad0 36 __gconv_t result = NULL;
6973fc01
UD
37 size_t cnt = 0;
38 int res;
306eeae5 39 int conv_flags = 0;
55985355 40 const char *errhand;
323fb88d 41 const char *ignore;
ba7b4d29 42 bool translit = false;
85830c4c 43
55985355
UD
44 /* Find out whether any error handling method is specified. */
45 errhand = strchr (toset, '/');
46 if (errhand != NULL)
47 errhand = strchr (errhand + 1, '/');
a1ffb40e 48 if (__glibc_likely (errhand != NULL))
85830c4c 49 {
323fb88d 50 if (*++errhand == '\0')
55985355
UD
51 errhand = NULL;
52 else
53 {
54 /* Make copy without the error handling description. */
55 char *newtoset = (char *) alloca (errhand - toset + 1);
d6204268 56 char *tok;
9d9febc7 57 char *ptr = NULL /* Work around a bogus warning */;
85830c4c 58
55985355
UD
59 newtoset[errhand - toset] = '\0';
60 toset = memcpy (newtoset, toset, errhand - toset);
85830c4c 61
d6204268
UD
62 /* Find the appropriate transliteration handlers. */
63 tok = strdupa (errhand);
55985355 64
d6204268
UD
65 tok = __strtok_r (tok, ",", &ptr);
66 while (tok != NULL)
55985355 67 {
4b5b009c 68 if (__strcasecmp_l (tok, "TRANSLIT", _nl_C_locobj_ptr) == 0)
ba7b4d29 69 translit = true;
4b5b009c 70 else if (__strcasecmp_l (tok, "IGNORE", _nl_C_locobj_ptr) == 0)
d6204268 71 /* Set the flag to ignore all errors. */
306eeae5 72 conv_flags |= __GCONV_IGNORE_ERRORS;
d6204268
UD
73
74 tok = __strtok_r (NULL, ",", &ptr);
55985355
UD
75 }
76 }
85830c4c 77 }
6973fc01 78
323fb88d
UD
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')
84 {
85 char *newfromset = (char *) alloca (ignore - fromset + 1);
86
87 newfromset[ignore - fromset] = '\0';
88 fromset = memcpy (newfromset, fromset, ignore - fromset);
89 }
90
e0e86ccb
UD
91 /* If the string is empty define this to mean the charset of the
92 currently selected locale. */
93 if (strcmp (toset, "//") == 0)
94 {
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);
100 }
101 if (strcmp (fromset, "//") == 0)
102 {
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);
108 }
109
c90a2db6 110 res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
d64b6ad0 111 if (res == __GCONV_OK)
6973fc01
UD
112 {
113 /* Allocate room for handle. */
d64b6ad0
UD
114 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
115 + (nsteps
116 * sizeof (struct __gconv_step_data)));
6973fc01 117 if (result == NULL)
d64b6ad0 118 res = __GCONV_NOMEM;
6973fc01
UD
119 else
120 {
121 /* Remember the list of steps. */
d64b6ad0
UD
122 result->__steps = steps;
123 result->__nsteps = nsteps;
6973fc01 124
390500b1 125 /* Clear the array for the step data. */
d64b6ad0
UD
126 memset (result->__data, '\0',
127 nsteps * sizeof (struct __gconv_step_data));
6973fc01 128
390500b1 129 /* Call all initialization functions for the transformation
49c091e5 130 step implementations. */
405b8c60 131 for (cnt = 0; cnt < nsteps; ++cnt)
6973fc01 132 {
0aece08d
UD
133 size_t size;
134
8b682b99
UD
135 /* Would have to be done if we would not clear the whole
136 array above. */
85830c4c 137#if 0
390500b1 138 /* Reset the counter. */
d64b6ad0 139 result->__data[cnt].__invocation_counter = 0;
6973fc01 140
390500b1 141 /* It's a regular use. */
d64b6ad0 142 result->__data[cnt].__internal_use = 0;
85830c4c 143#endif
e3e0a182 144
390500b1 145 /* We use the `mbstate_t' member in DATA. */
d64b6ad0 146 result->__data[cnt].__statep = &result->__data[cnt].__state;
e3e0a182 147
ba7b4d29
FW
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;
0aece08d 154
405b8c60
UD
155 /* If this is the last step we must not allocate an
156 output buffer. */
157 if (cnt < nsteps - 1)
d6204268 158 {
306eeae5 159 result->__data[cnt].__flags = conv_flags;
d6204268 160
405b8c60
UD
161 /* Allocate the buffer. */
162 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
d6204268 163
9cfe5381 164 result->__data[cnt].__outbuf = malloc (size);
405b8c60 165 if (result->__data[cnt].__outbuf == NULL)
67aacae6
UD
166 {
167 res = __GCONV_NOMEM;
168 goto bail;
169 }
d6204268 170
405b8c60
UD
171 result->__data[cnt].__outbufend =
172 result->__data[cnt].__outbuf + size;
173 }
174 else
175 {
176 /* Handle the last entry. */
306eeae5 177 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
7039a4c9 178
d6204268
UD
179 break;
180 }
405b8c60 181 }
6973fc01 182 }
6973fc01 183
45eca4d1 184 if (res != __GCONV_OK)
6973fc01 185 {
45eca4d1 186 /* Something went wrong. Free all the resources. */
d6204268
UD
187 int serrno;
188 bail:
189 serrno = errno;
6973fc01 190
45eca4d1
UD
191 if (result != NULL)
192 {
193 while (cnt-- > 0)
ba7b4d29 194 free (result->__data[cnt].__outbuf);
45eca4d1
UD
195
196 free (result);
197 result = NULL;
198 }
6973fc01 199
45eca4d1 200 __gconv_close_transform (steps, nsteps);
6973fc01 201
45eca4d1
UD
202 __set_errno (serrno);
203 }
6973fc01
UD
204 }
205
206 *handle = result;
207 return res;
208}