lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1997-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _GCONV_INT_H |
| 20 | #define _GCONV_INT_H 1 |
| 21 | |
| 22 | #include "gconv.h" |
| 23 | #include <stdlib.h> /* For alloca used in macro below. */ |
| 24 | #include <ctype.h> /* For __toupper_l used in macro below. */ |
| 25 | #include <string.h> /* For strlen et al used in macro below. */ |
| 26 | #include <bits/libc-lock.h> |
| 27 | |
| 28 | __BEGIN_DECLS |
| 29 | |
| 30 | |
| 31 | /* Type to represent search path. */ |
| 32 | struct path_elem |
| 33 | { |
| 34 | const char *name; |
| 35 | size_t len; |
| 36 | }; |
| 37 | |
| 38 | /* Variable with search path for `gconv' implementation. */ |
| 39 | extern struct path_elem *__gconv_path_elem attribute_hidden; |
| 40 | /* Maximum length of a single path element. */ |
| 41 | extern size_t __gconv_max_path_elem_len attribute_hidden; |
| 42 | |
| 43 | |
| 44 | /* Structure for alias definition. Simply two strings. */ |
| 45 | struct gconv_alias |
| 46 | { |
| 47 | char *fromname; |
| 48 | char *toname; |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | /* How many character should be converted in one call? */ |
| 53 | #define GCONV_NCHAR_GOAL 8160 |
| 54 | |
| 55 | |
| 56 | /* Structure describing one loaded shared object. This normally are |
| 57 | objects to perform conversation but as a special case the db shared |
| 58 | object is also handled. */ |
| 59 | struct __gconv_loaded_object |
| 60 | { |
| 61 | /* Name of the object. It must be the first structure element. */ |
| 62 | const char *name; |
| 63 | |
| 64 | /* Reference counter for the db functionality. If no conversion is |
| 65 | needed we unload the db library. */ |
| 66 | int counter; |
| 67 | |
| 68 | /* The handle for the shared object. */ |
| 69 | void *handle; |
| 70 | |
| 71 | /* Pointer to the functions the module defines. */ |
| 72 | __gconv_fct fct; |
| 73 | __gconv_init_fct init_fct; |
| 74 | __gconv_end_fct end_fct; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | /* Description for an available conversion module. */ |
| 79 | struct gconv_module |
| 80 | { |
| 81 | const char *from_string; |
| 82 | const char *to_string; |
| 83 | |
| 84 | int cost_hi; |
| 85 | int cost_lo; |
| 86 | |
| 87 | const char *module_name; |
| 88 | |
| 89 | struct gconv_module *left; /* Prefix smaller. */ |
| 90 | struct gconv_module *same; /* List of entries with identical prefix. */ |
| 91 | struct gconv_module *right; /* Prefix larger. */ |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | /* Flags for `gconv_open'. */ |
| 96 | enum |
| 97 | { |
| 98 | GCONV_AVOID_NOCONV = 1 << 0 |
| 99 | }; |
| 100 | |
| 101 | /* When GCONV_AVOID_NOCONV is set and no conversion is needed, |
| 102 | __GCONV_NULCONV should be returned. */ |
| 103 | enum |
| 104 | { |
| 105 | __GCONV_NULCONV = -1 |
| 106 | }; |
| 107 | |
| 108 | /* Global variables. */ |
| 109 | |
| 110 | /* Database of alias names. */ |
| 111 | extern void *__gconv_alias_db attribute_hidden; |
| 112 | |
| 113 | /* Array with available modules. */ |
| 114 | extern size_t __gconv_nmodules; |
| 115 | extern struct gconv_module *__gconv_modules_db attribute_hidden; |
| 116 | |
| 117 | /* Value of the GCONV_PATH environment variable. */ |
| 118 | extern const char *__gconv_path_envvar attribute_hidden; |
| 119 | |
| 120 | /* Lock for the conversion database content. */ |
| 121 | __libc_lock_define (extern, __gconv_lock attribute_hidden) |
| 122 | |
| 123 | |
| 124 | /* The gconv functions expects the name to be in upper case and complete, |
| 125 | including the trailing slashes if necessary. */ |
| 126 | #define norm_add_slashes(str,suffix) \ |
| 127 | ({ \ |
| 128 | const char *cp = (str); \ |
| 129 | char *result; \ |
| 130 | char *tmp; \ |
| 131 | size_t cnt = 0; \ |
| 132 | const size_t suffix_len = strlen (suffix); \ |
| 133 | \ |
| 134 | while (*cp != '\0') \ |
| 135 | if (*cp++ == '/') \ |
| 136 | ++cnt; \ |
| 137 | \ |
| 138 | tmp = result = __alloca (cp - (str) + 3 + suffix_len); \ |
| 139 | cp = (str); \ |
| 140 | while (*cp != '\0') \ |
| 141 | *tmp++ = __toupper_l (*cp++, _nl_C_locobj_ptr); \ |
| 142 | if (cnt < 2) \ |
| 143 | { \ |
| 144 | *tmp++ = '/'; \ |
| 145 | if (cnt < 1) \ |
| 146 | { \ |
| 147 | *tmp++ = '/'; \ |
| 148 | if (suffix_len != 0) \ |
| 149 | tmp = __mempcpy (tmp, suffix, suffix_len); \ |
| 150 | } \ |
| 151 | } \ |
| 152 | *tmp = '\0'; \ |
| 153 | result; \ |
| 154 | }) |
| 155 | |
| 156 | |
| 157 | /* Return in *HANDLE decriptor for transformation from FROMSET to TOSET. */ |
| 158 | extern int __gconv_open (const char *toset, const char *fromset, |
| 159 | __gconv_t *handle, int flags) |
| 160 | internal_function; |
| 161 | |
| 162 | /* Free resources associated with transformation descriptor CD. */ |
| 163 | extern int __gconv_close (__gconv_t cd) |
| 164 | internal_function; |
| 165 | |
| 166 | /* Transform at most *INBYTESLEFT bytes from buffer starting at *INBUF |
| 167 | according to rules described by CD and place up to *OUTBYTESLEFT |
| 168 | bytes in buffer starting at *OUTBUF. Return number of non-identical |
| 169 | conversions in *IRREVERSIBLE if this pointer is not null. */ |
| 170 | extern int __gconv (__gconv_t cd, const unsigned char **inbuf, |
| 171 | const unsigned char *inbufend, unsigned char **outbuf, |
| 172 | unsigned char *outbufend, size_t *irreversible) |
| 173 | internal_function; |
| 174 | |
| 175 | /* Return in *HANDLE a pointer to an array with *NSTEPS elements describing |
| 176 | the single steps necessary for transformation from FROMSET to TOSET. */ |
| 177 | extern int __gconv_find_transform (const char *toset, const char *fromset, |
| 178 | struct __gconv_step **handle, |
| 179 | size_t *nsteps, int flags) |
| 180 | internal_function; |
| 181 | |
| 182 | /* Search for transformation in cache data. */ |
| 183 | extern int __gconv_lookup_cache (const char *toset, const char *fromset, |
| 184 | struct __gconv_step **handle, size_t *nsteps, |
| 185 | int flags) |
| 186 | internal_function; |
| 187 | |
| 188 | /* Compare the two name for whether they are after alias expansion the |
| 189 | same. This function uses the cache and fails if none is |
| 190 | loaded. */ |
| 191 | extern int __gconv_compare_alias_cache (const char *name1, const char *name2, |
| 192 | int *result) internal_function; |
| 193 | |
| 194 | /* Free data associated with a step's structure. */ |
| 195 | extern void __gconv_release_step (struct __gconv_step *step) |
| 196 | internal_function; |
| 197 | |
| 198 | /* Read all the configuration data and cache it. */ |
| 199 | extern void __gconv_read_conf (void) attribute_hidden; |
| 200 | |
| 201 | /* Try to read module cache file. */ |
| 202 | extern int __gconv_load_cache (void) internal_function; |
| 203 | |
| 204 | /* Retrieve pointer to internal cache. */ |
| 205 | extern void *__gconv_get_cache (void); |
| 206 | |
| 207 | /* Retrieve pointer to internal module database. */ |
| 208 | extern struct gconv_module *__gconv_get_modules_db (void); |
| 209 | |
| 210 | /* Retrieve pointer to internal alias database. */ |
| 211 | extern void *__gconv_get_alias_db (void); |
| 212 | |
| 213 | /* Determine the directories we are looking in. */ |
| 214 | extern void __gconv_get_path (void) internal_function; |
| 215 | |
| 216 | /* Comparison function to search alias. */ |
| 217 | extern int __gconv_alias_compare (const void *p1, const void *p2) |
| 218 | attribute_hidden; |
| 219 | |
| 220 | /* Clear reference to transformation step implementations which might |
| 221 | cause the code to be unloaded. */ |
| 222 | extern int __gconv_close_transform (struct __gconv_step *steps, |
| 223 | size_t nsteps) |
| 224 | internal_function; |
| 225 | |
| 226 | /* Free all resources allocated for the transformation record when |
| 227 | using the cache. */ |
| 228 | extern void __gconv_release_cache (struct __gconv_step *steps, size_t nsteps) |
| 229 | internal_function; |
| 230 | |
| 231 | /* Load shared object named by NAME. If already loaded increment reference |
| 232 | count. */ |
| 233 | extern struct __gconv_loaded_object *__gconv_find_shlib (const char *name) |
| 234 | internal_function; |
| 235 | |
| 236 | /* Release shared object. If no further reference is available unload |
| 237 | the object. */ |
| 238 | extern void __gconv_release_shlib (struct __gconv_loaded_object *handle) |
| 239 | internal_function; |
| 240 | |
| 241 | /* Fill STEP with information about builtin module with NAME. */ |
| 242 | extern void __gconv_get_builtin_trans (const char *name, |
| 243 | struct __gconv_step *step) |
| 244 | internal_function; |
| 245 | |
| 246 | libc_hidden_proto (__gconv_transliterate) |
| 247 | |
| 248 | /* If NAME is an codeset alias expand it. */ |
| 249 | extern int __gconv_compare_alias (const char *name1, const char *name2) |
| 250 | internal_function; |
| 251 | |
| 252 | |
| 253 | /* Builtin transformations. */ |
| 254 | #ifdef _LIBC |
| 255 | # define __BUILTIN_TRANSFORM(Name) \ |
| 256 | extern int Name (struct __gconv_step *step, \ |
| 257 | struct __gconv_step_data *data, \ |
| 258 | const unsigned char **inbuf, \ |
| 259 | const unsigned char *inbufend, \ |
| 260 | unsigned char **outbufstart, size_t *irreversible, \ |
| 261 | int do_flush, int consume_incomplete) |
| 262 | |
| 263 | __BUILTIN_TRANSFORM (__gconv_transform_ascii_internal); |
| 264 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ascii); |
| 265 | __BUILTIN_TRANSFORM (__gconv_transform_utf8_internal); |
| 266 | __BUILTIN_TRANSFORM (__gconv_transform_internal_utf8); |
| 267 | __BUILTIN_TRANSFORM (__gconv_transform_ucs2_internal); |
| 268 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2); |
| 269 | __BUILTIN_TRANSFORM (__gconv_transform_ucs2reverse_internal); |
| 270 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2reverse); |
| 271 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4); |
| 272 | __BUILTIN_TRANSFORM (__gconv_transform_ucs4_internal); |
| 273 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4le); |
| 274 | __BUILTIN_TRANSFORM (__gconv_transform_ucs4le_internal); |
| 275 | __BUILTIN_TRANSFORM (__gconv_transform_internal_utf16); |
| 276 | __BUILTIN_TRANSFORM (__gconv_transform_utf16_internal); |
| 277 | # undef __BUITLIN_TRANSFORM |
| 278 | |
| 279 | /* Specialized conversion function for a single byte to INTERNAL, recognizing |
| 280 | only ASCII characters. */ |
| 281 | extern wint_t __gconv_btwoc_ascii (struct __gconv_step *step, unsigned char c); |
| 282 | |
| 283 | #endif |
| 284 | |
| 285 | __END_DECLS |
| 286 | |
| 287 | #endif /* gconv_int.h */ |