]> 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.
2b778ceb 2 Copyright (C) 1997-2021 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 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://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 28
335a3b0a
AS
29/* How many character should be converted in one call? */
30#define GCONV_NCHAR_GOAL 8160
31
32
6973fc01 33int
91927b7c 34__gconv_open (struct gconv_spec *conv_spec, __gconv_t *handle,
c90a2db6 35 int flags)
6973fc01 36{
d64b6ad0 37 struct __gconv_step *steps;
6973fc01 38 size_t nsteps;
d64b6ad0 39 __gconv_t result = NULL;
6973fc01
UD
40 size_t cnt = 0;
41 int res;
306eeae5 42 int conv_flags = 0;
ba7b4d29 43 bool translit = false;
91927b7c 44 char *tocode, *fromcode;
85830c4c 45
55985355 46 /* Find out whether any error handling method is specified. */
91927b7c 47 translit = conv_spec->translit;
85830c4c 48
91927b7c
AS
49 if (conv_spec->ignore)
50 conv_flags |= __GCONV_IGNORE_ERRORS;
55985355 51
91927b7c
AS
52 tocode = conv_spec->tocode;
53 fromcode = conv_spec->fromcode;
323fb88d 54
e0e86ccb
UD
55 /* If the string is empty define this to mean the charset of the
56 currently selected locale. */
91927b7c 57 if (strcmp (tocode, "//") == 0)
e0e86ccb
UD
58 {
59 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
60 size_t len = strlen (codeset);
61 char *dest;
91927b7c 62 tocode = dest = (char *) alloca (len + 3);
e0e86ccb
UD
63 memcpy (__mempcpy (dest, codeset, len), "//", 3);
64 }
91927b7c 65 if (strcmp (fromcode, "//") == 0)
e0e86ccb
UD
66 {
67 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
68 size_t len = strlen (codeset);
69 char *dest;
91927b7c 70 fromcode = dest = (char *) alloca (len + 3);
e0e86ccb
UD
71 memcpy (__mempcpy (dest, codeset, len), "//", 3);
72 }
73
91927b7c 74 res = __gconv_find_transform (tocode, fromcode, &steps, &nsteps, flags);
d64b6ad0 75 if (res == __GCONV_OK)
6973fc01
UD
76 {
77 /* Allocate room for handle. */
d64b6ad0
UD
78 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
79 + (nsteps
80 * sizeof (struct __gconv_step_data)));
6973fc01 81 if (result == NULL)
d64b6ad0 82 res = __GCONV_NOMEM;
6973fc01
UD
83 else
84 {
85 /* Remember the list of steps. */
d64b6ad0
UD
86 result->__steps = steps;
87 result->__nsteps = nsteps;
6973fc01 88
390500b1 89 /* Clear the array for the step data. */
d64b6ad0
UD
90 memset (result->__data, '\0',
91 nsteps * sizeof (struct __gconv_step_data));
6973fc01 92
390500b1 93 /* Call all initialization functions for the transformation
49c091e5 94 step implementations. */
405b8c60 95 for (cnt = 0; cnt < nsteps; ++cnt)
6973fc01 96 {
0aece08d
UD
97 size_t size;
98
8b682b99
UD
99 /* Would have to be done if we would not clear the whole
100 array above. */
85830c4c 101#if 0
390500b1 102 /* Reset the counter. */
d64b6ad0 103 result->__data[cnt].__invocation_counter = 0;
6973fc01 104
390500b1 105 /* It's a regular use. */
d64b6ad0 106 result->__data[cnt].__internal_use = 0;
85830c4c 107#endif
e3e0a182 108
390500b1 109 /* We use the `mbstate_t' member in DATA. */
d64b6ad0 110 result->__data[cnt].__statep = &result->__data[cnt].__state;
e3e0a182 111
ba7b4d29
FW
112 /* The builtin transliteration handling only
113 supports the internal encoding. */
114 if (translit
115 && __strcasecmp_l (steps[cnt].__from_name,
116 "INTERNAL", _nl_C_locobj_ptr) == 0)
117 conv_flags |= __GCONV_TRANSLIT;
0aece08d 118
405b8c60
UD
119 /* If this is the last step we must not allocate an
120 output buffer. */
121 if (cnt < nsteps - 1)
d6204268 122 {
306eeae5 123 result->__data[cnt].__flags = conv_flags;
d6204268 124
405b8c60
UD
125 /* Allocate the buffer. */
126 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
d6204268 127
9cfe5381 128 result->__data[cnt].__outbuf = malloc (size);
405b8c60 129 if (result->__data[cnt].__outbuf == NULL)
67aacae6
UD
130 {
131 res = __GCONV_NOMEM;
132 goto bail;
133 }
d6204268 134
405b8c60
UD
135 result->__data[cnt].__outbufend =
136 result->__data[cnt].__outbuf + size;
137 }
138 else
139 {
140 /* Handle the last entry. */
306eeae5 141 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
7039a4c9 142
d6204268
UD
143 break;
144 }
405b8c60 145 }
6973fc01 146 }
6973fc01 147
45eca4d1 148 if (res != __GCONV_OK)
6973fc01 149 {
45eca4d1 150 /* Something went wrong. Free all the resources. */
d6204268
UD
151 int serrno;
152 bail:
153 serrno = errno;
6973fc01 154
45eca4d1
UD
155 if (result != NULL)
156 {
157 while (cnt-- > 0)
ba7b4d29 158 free (result->__data[cnt].__outbuf);
45eca4d1
UD
159
160 free (result);
161 result = NULL;
162 }
6973fc01 163
45eca4d1 164 __gconv_close_transform (steps, nsteps);
6973fc01 165
45eca4d1
UD
166 __set_errno (serrno);
167 }
6973fc01
UD
168 }
169
170 *handle = result;
171 return res;
172}
91927b7c 173libc_hidden_def (__gconv_open)