| 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. | 
|---|
| 5 |  | 
|---|
| 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. | 
|---|
| 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 | 
|---|
| 14 | Lesser General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 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/>.  */ | 
|---|
| 19 |  | 
|---|
| 20 | #include <errno.h> | 
|---|
| 21 | #include <locale.h> | 
|---|
| 22 | #include "../locale/localeinfo.h" | 
|---|
| 23 | #include <stdlib.h> | 
|---|
| 24 | #include <string.h> | 
|---|
| 25 |  | 
|---|
| 26 | #include <gconv_int.h> | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | /* How many character should be converted in one call?  */ | 
|---|
| 30 | #define GCONV_NCHAR_GOAL	8160 | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 | int | 
|---|
| 34 | __gconv_open (struct gconv_spec *conv_spec, __gconv_t *handle, | 
|---|
| 35 | int flags) | 
|---|
| 36 | { | 
|---|
| 37 | struct __gconv_step *steps; | 
|---|
| 38 | size_t nsteps; | 
|---|
| 39 | __gconv_t result = NULL; | 
|---|
| 40 | size_t cnt = 0; | 
|---|
| 41 | int res; | 
|---|
| 42 | int conv_flags = 0; | 
|---|
| 43 | bool translit = false; | 
|---|
| 44 | char *tocode, *fromcode; | 
|---|
| 45 |  | 
|---|
| 46 | /* Find out whether any error handling method is specified.  */ | 
|---|
| 47 | translit = conv_spec->translit; | 
|---|
| 48 |  | 
|---|
| 49 | if (conv_spec->ignore) | 
|---|
| 50 | conv_flags |= __GCONV_IGNORE_ERRORS; | 
|---|
| 51 |  | 
|---|
| 52 | tocode = conv_spec->tocode; | 
|---|
| 53 | fromcode = conv_spec->fromcode; | 
|---|
| 54 |  | 
|---|
| 55 | /* If the string is empty define this to mean the charset of the | 
|---|
| 56 | currently selected locale.  */ | 
|---|
| 57 | if (strcmp (tocode, "//") == 0) | 
|---|
| 58 | { | 
|---|
| 59 | const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET); | 
|---|
| 60 | size_t len = strlen (codeset); | 
|---|
| 61 | char *dest; | 
|---|
| 62 | tocode = dest = (char *) alloca (len + 3); | 
|---|
| 63 | memcpy (__mempcpy (dest, codeset, len), "//", 3); | 
|---|
| 64 | } | 
|---|
| 65 | if (strcmp (fromcode, "//") == 0) | 
|---|
| 66 | { | 
|---|
| 67 | const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET); | 
|---|
| 68 | size_t len = strlen (codeset); | 
|---|
| 69 | char *dest; | 
|---|
| 70 | fromcode = dest = (char *) alloca (len + 3); | 
|---|
| 71 | memcpy (__mempcpy (dest, codeset, len), "//", 3); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | res = __gconv_find_transform (tocode, fromcode, &steps, &nsteps, flags); | 
|---|
| 75 | if (res == __GCONV_OK) | 
|---|
| 76 | { | 
|---|
| 77 | /* Allocate room for handle.  */ | 
|---|
| 78 | result = (__gconv_t) malloc (sizeof (struct __gconv_info) | 
|---|
| 79 | + (nsteps | 
|---|
| 80 | * sizeof (struct __gconv_step_data))); | 
|---|
| 81 | if (result == NULL) | 
|---|
| 82 | res = __GCONV_NOMEM; | 
|---|
| 83 | else | 
|---|
| 84 | { | 
|---|
| 85 | /* Remember the list of steps.  */ | 
|---|
| 86 | result->__steps = steps; | 
|---|
| 87 | result->__nsteps = nsteps; | 
|---|
| 88 |  | 
|---|
| 89 | /* Clear the array for the step data.  */ | 
|---|
| 90 | memset (result->__data, '\0', | 
|---|
| 91 | nsteps * sizeof (struct __gconv_step_data)); | 
|---|
| 92 |  | 
|---|
| 93 | /* Call all initialization functions for the transformation | 
|---|
| 94 | step implementations.  */ | 
|---|
| 95 | for (cnt = 0; cnt < nsteps; ++cnt) | 
|---|
| 96 | { | 
|---|
| 97 | size_t size; | 
|---|
| 98 |  | 
|---|
| 99 | /* Would have to be done if we would not clear the whole | 
|---|
| 100 | array above.  */ | 
|---|
| 101 | #if 0 | 
|---|
| 102 | /* Reset the counter.  */ | 
|---|
| 103 | result->__data[cnt].__invocation_counter = 0; | 
|---|
| 104 |  | 
|---|
| 105 | /* It's a regular use.  */ | 
|---|
| 106 | result->__data[cnt].__internal_use = 0; | 
|---|
| 107 | #endif | 
|---|
| 108 |  | 
|---|
| 109 | /* We use the `mbstate_t' member in DATA.  */ | 
|---|
| 110 | result->__data[cnt].__statep = &result->__data[cnt].__state; | 
|---|
| 111 |  | 
|---|
| 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; | 
|---|
| 118 |  | 
|---|
| 119 | /* If this is the last step we must not allocate an | 
|---|
| 120 | output buffer.  */ | 
|---|
| 121 | if (cnt < nsteps - 1) | 
|---|
| 122 | { | 
|---|
| 123 | result->__data[cnt].__flags = conv_flags; | 
|---|
| 124 |  | 
|---|
| 125 | /* Allocate the buffer.  */ | 
|---|
| 126 | size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to); | 
|---|
| 127 |  | 
|---|
| 128 | result->__data[cnt].__outbuf = malloc (size); | 
|---|
| 129 | if (result->__data[cnt].__outbuf == NULL) | 
|---|
| 130 | { | 
|---|
| 131 | res = __GCONV_NOMEM; | 
|---|
| 132 | goto bail; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | result->__data[cnt].__outbufend = | 
|---|
| 136 | result->__data[cnt].__outbuf + size; | 
|---|
| 137 | } | 
|---|
| 138 | else | 
|---|
| 139 | { | 
|---|
| 140 | /* Handle the last entry.  */ | 
|---|
| 141 | result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST; | 
|---|
| 142 |  | 
|---|
| 143 | break; | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | if (res != __GCONV_OK) | 
|---|
| 149 | { | 
|---|
| 150 | /* Something went wrong.  Free all the resources.  */ | 
|---|
| 151 | int serrno; | 
|---|
| 152 | bail: | 
|---|
| 153 | serrno = errno; | 
|---|
| 154 |  | 
|---|
| 155 | if (result != NULL) | 
|---|
| 156 | { | 
|---|
| 157 | while (cnt-- > 0) | 
|---|
| 158 | free (result->__data[cnt].__outbuf); | 
|---|
| 159 |  | 
|---|
| 160 | free (result); | 
|---|
| 161 | result = NULL; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | __gconv_close_transform (steps, nsteps); | 
|---|
| 165 |  | 
|---|
| 166 | __set_errno (serrno); | 
|---|
| 167 | } | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | *handle = result; | 
|---|
| 171 | return res; | 
|---|
| 172 | } | 
|---|
| 173 | libc_hidden_def (__gconv_open) | 
|---|
| 174 |  | 
|---|