| 1 | /* Copyright (C) 1997-2020 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 | <https://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 <libc-lock.h> | 
|---|
| 27 |  | 
|---|
| 28 | __BEGIN_DECLS | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | /* Structure for alias definition.  Simply two strings.  */ | 
|---|
| 32 | struct gconv_alias | 
|---|
| 33 | { | 
|---|
| 34 | char *fromname; | 
|---|
| 35 | char *toname; | 
|---|
| 36 | }; | 
|---|
| 37 |  | 
|---|
| 38 |  | 
|---|
| 39 | /* Structure describing one loaded shared object.  This normally are | 
|---|
| 40 | objects to perform conversation but as a special case the db shared | 
|---|
| 41 | object is also handled.  */ | 
|---|
| 42 | struct __gconv_loaded_object | 
|---|
| 43 | { | 
|---|
| 44 | /* Name of the object.  It must be the first structure element.  */ | 
|---|
| 45 | const char *name; | 
|---|
| 46 |  | 
|---|
| 47 | /* Reference counter for the db functionality.  If no conversion is | 
|---|
| 48 | needed we unload the db library.  */ | 
|---|
| 49 | int counter; | 
|---|
| 50 |  | 
|---|
| 51 | /* The handle for the shared object.  */ | 
|---|
| 52 | void *handle; | 
|---|
| 53 |  | 
|---|
| 54 | /* Pointer to the functions the module defines.  */ | 
|---|
| 55 | __gconv_fct fct; | 
|---|
| 56 | __gconv_init_fct init_fct; | 
|---|
| 57 | __gconv_end_fct end_fct; | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | /* Description for an available conversion module.  */ | 
|---|
| 62 | struct gconv_module | 
|---|
| 63 | { | 
|---|
| 64 | const char *from_string; | 
|---|
| 65 | const char *to_string; | 
|---|
| 66 |  | 
|---|
| 67 | int cost_hi; | 
|---|
| 68 | int cost_lo; | 
|---|
| 69 |  | 
|---|
| 70 | const char *module_name; | 
|---|
| 71 |  | 
|---|
| 72 | struct gconv_module *left;	/* Prefix smaller.  */ | 
|---|
| 73 | struct gconv_module *same;	/* List of entries with identical prefix.  */ | 
|---|
| 74 | struct gconv_module *right;	/* Prefix larger.  */ | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | /* The specification of the conversion that needs to be performed.  */ | 
|---|
| 79 | struct gconv_spec | 
|---|
| 80 | { | 
|---|
| 81 | char *fromcode; | 
|---|
| 82 | char *tocode; | 
|---|
| 83 | bool translit; | 
|---|
| 84 | bool ignore; | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 | /* Flags for `gconv_open'.  */ | 
|---|
| 88 | enum | 
|---|
| 89 | { | 
|---|
| 90 | GCONV_AVOID_NOCONV = 1 << 0 | 
|---|
| 91 | }; | 
|---|
| 92 |  | 
|---|
| 93 | /* When GCONV_AVOID_NOCONV is set and no conversion is needed, | 
|---|
| 94 | __GCONV_NULCONV should be returned.  */ | 
|---|
| 95 | enum | 
|---|
| 96 | { | 
|---|
| 97 | __GCONV_NULCONV = -1 | 
|---|
| 98 | }; | 
|---|
| 99 |  | 
|---|
| 100 | /* Global variables.  */ | 
|---|
| 101 |  | 
|---|
| 102 | /* Database of alias names.  */ | 
|---|
| 103 | extern void *__gconv_alias_db attribute_hidden; | 
|---|
| 104 |  | 
|---|
| 105 | /* Array with available modules.  */ | 
|---|
| 106 | extern struct gconv_module *__gconv_modules_db attribute_hidden; | 
|---|
| 107 |  | 
|---|
| 108 | /* Value of the GCONV_PATH environment variable.  */ | 
|---|
| 109 | extern const char *__gconv_path_envvar attribute_hidden; | 
|---|
| 110 |  | 
|---|
| 111 | /* Lock for the conversion database content.  */ | 
|---|
| 112 | __libc_lock_define (extern, __gconv_lock attribute_hidden) | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 | /* The gconv functions expects the name to be in upper case and complete, | 
|---|
| 116 | including the trailing slashes if necessary.  */ | 
|---|
| 117 | #define norm_add_slashes(str,suffix) \ | 
|---|
| 118 | ({									      \ | 
|---|
| 119 | const char *cp = (str);						      \ | 
|---|
| 120 | char *result;							      \ | 
|---|
| 121 | char *tmp;								      \ | 
|---|
| 122 | size_t cnt = 0;							      \ | 
|---|
| 123 | const size_t suffix_len = strlen (suffix);				      \ | 
|---|
| 124 | \ | 
|---|
| 125 | while (*cp != '\0')							      \ | 
|---|
| 126 | if (*cp++ == '/')							      \ | 
|---|
| 127 | ++cnt;								      \ | 
|---|
| 128 | \ | 
|---|
| 129 | tmp = result = __alloca (cp - (str) + 3 + suffix_len);		      \ | 
|---|
| 130 | cp = (str);								      \ | 
|---|
| 131 | while (*cp != '\0')							      \ | 
|---|
| 132 | *tmp++ = __toupper_l (*cp++, _nl_C_locobj_ptr);			      \ | 
|---|
| 133 | if (cnt < 2)							      \ | 
|---|
| 134 | {									      \ | 
|---|
| 135 | *tmp++ = '/';							      \ | 
|---|
| 136 | if (cnt < 1)							      \ | 
|---|
| 137 | {								      \ | 
|---|
| 138 | *tmp++ = '/';						      \ | 
|---|
| 139 | if (suffix_len != 0)					      \ | 
|---|
| 140 | tmp = __mempcpy (tmp, suffix, suffix_len);		      \ | 
|---|
| 141 | }								      \ | 
|---|
| 142 | }									      \ | 
|---|
| 143 | *tmp = '\0';							      \ | 
|---|
| 144 | result;								      \ | 
|---|
| 145 | }) | 
|---|
| 146 |  | 
|---|
| 147 |  | 
|---|
| 148 | /* Return in *HANDLE, a decriptor for the transformation.  The function expects | 
|---|
| 149 | the specification of the transformation in the structure pointed to by | 
|---|
| 150 | CONV_SPEC.  It only reads *CONV_SPEC and does not take ownership of it.  */ | 
|---|
| 151 | extern int __gconv_open (struct gconv_spec *conv_spec, | 
|---|
| 152 | __gconv_t *handle, int flags); | 
|---|
| 153 | libc_hidden_proto (__gconv_open) | 
|---|
| 154 |  | 
|---|
| 155 | /* Free resources associated with transformation descriptor CD.  */ | 
|---|
| 156 | extern int __gconv_close (__gconv_t cd) | 
|---|
| 157 | attribute_hidden; | 
|---|
| 158 |  | 
|---|
| 159 | /* Transform at most *INBYTESLEFT bytes from buffer starting at *INBUF | 
|---|
| 160 | according to rules described by CD and place up to *OUTBYTESLEFT | 
|---|
| 161 | bytes in buffer starting at *OUTBUF.  Return number of non-identical | 
|---|
| 162 | conversions in *IRREVERSIBLE if this pointer is not null.  */ | 
|---|
| 163 | extern int __gconv (__gconv_t cd, const unsigned char **inbuf, | 
|---|
| 164 | const unsigned char *inbufend, unsigned char **outbuf, | 
|---|
| 165 | unsigned char *outbufend, size_t *irreversible) | 
|---|
| 166 | attribute_hidden; | 
|---|
| 167 |  | 
|---|
| 168 | /* Return in *HANDLE a pointer to an array with *NSTEPS elements describing | 
|---|
| 169 | the single steps necessary for transformation from FROMSET to TOSET.  */ | 
|---|
| 170 | extern int __gconv_find_transform (const char *toset, const char *fromset, | 
|---|
| 171 | struct __gconv_step **handle, | 
|---|
| 172 | size_t *nsteps, int flags) | 
|---|
| 173 | attribute_hidden; | 
|---|
| 174 |  | 
|---|
| 175 | /* Search for transformation in cache data.  */ | 
|---|
| 176 | extern int __gconv_lookup_cache (const char *toset, const char *fromset, | 
|---|
| 177 | struct __gconv_step **handle, size_t *nsteps, | 
|---|
| 178 | int flags) | 
|---|
| 179 | attribute_hidden; | 
|---|
| 180 |  | 
|---|
| 181 | /* Compare the two name for whether they are after alias expansion the | 
|---|
| 182 | same.  This function uses the cache and fails if none is | 
|---|
| 183 | loaded.  */ | 
|---|
| 184 | extern int __gconv_compare_alias_cache (const char *name1, const char *name2, | 
|---|
| 185 | int *result) | 
|---|
| 186 | attribute_hidden; | 
|---|
| 187 |  | 
|---|
| 188 | /* Free data associated with a step's structure.  */ | 
|---|
| 189 | extern void __gconv_release_step (struct __gconv_step *step) | 
|---|
| 190 | attribute_hidden; | 
|---|
| 191 |  | 
|---|
| 192 | /* Read all the configuration data and cache it if not done so already.  */ | 
|---|
| 193 | extern void __gconv_load_conf (void) attribute_hidden; | 
|---|
| 194 |  | 
|---|
| 195 | /* Try to read module cache file.  */ | 
|---|
| 196 | extern int __gconv_load_cache (void) attribute_hidden; | 
|---|
| 197 |  | 
|---|
| 198 | /* Retrieve pointer to internal cache.  */ | 
|---|
| 199 | extern void *__gconv_get_cache (void); | 
|---|
| 200 |  | 
|---|
| 201 | /* Retrieve pointer to internal module database.  */ | 
|---|
| 202 | extern struct gconv_module *__gconv_get_modules_db (void); | 
|---|
| 203 |  | 
|---|
| 204 | /* Retrieve pointer to internal alias database.  */ | 
|---|
| 205 | extern void *__gconv_get_alias_db (void); | 
|---|
| 206 |  | 
|---|
| 207 | /* Comparison function to search alias.  */ | 
|---|
| 208 | extern int __gconv_alias_compare (const void *p1, const void *p2) | 
|---|
| 209 | attribute_hidden; | 
|---|
| 210 |  | 
|---|
| 211 | /* Clear reference to transformation step implementations which might | 
|---|
| 212 | cause the code to be unloaded.  */ | 
|---|
| 213 | extern int __gconv_close_transform (struct __gconv_step *steps, | 
|---|
| 214 | size_t nsteps) | 
|---|
| 215 | attribute_hidden; | 
|---|
| 216 |  | 
|---|
| 217 | /* Free all resources allocated for the transformation record when | 
|---|
| 218 | using the cache.  */ | 
|---|
| 219 | extern void __gconv_release_cache (struct __gconv_step *steps, size_t nsteps) | 
|---|
| 220 | attribute_hidden; | 
|---|
| 221 |  | 
|---|
| 222 | /* Load shared object named by NAME.  If already loaded increment reference | 
|---|
| 223 | count.  */ | 
|---|
| 224 | extern struct __gconv_loaded_object *__gconv_find_shlib (const char *name) | 
|---|
| 225 | attribute_hidden; | 
|---|
| 226 |  | 
|---|
| 227 | /* Release shared object.  If no further reference is available unload | 
|---|
| 228 | the object.  */ | 
|---|
| 229 | extern void __gconv_release_shlib (struct __gconv_loaded_object *handle) | 
|---|
| 230 | attribute_hidden; | 
|---|
| 231 |  | 
|---|
| 232 | /* Fill STEP with information about builtin module with NAME.  */ | 
|---|
| 233 | extern void __gconv_get_builtin_trans (const char *name, | 
|---|
| 234 | struct __gconv_step *step) | 
|---|
| 235 | attribute_hidden; | 
|---|
| 236 |  | 
|---|
| 237 | /* Transliteration using the locale's data.  */ | 
|---|
| 238 | extern int __gconv_transliterate (struct __gconv_step *step, | 
|---|
| 239 | struct __gconv_step_data *step_data, | 
|---|
| 240 | const unsigned char *inbufstart, | 
|---|
| 241 | const unsigned char **inbufp, | 
|---|
| 242 | const unsigned char *inbufend, | 
|---|
| 243 | unsigned char **outbufstart, | 
|---|
| 244 | size_t *irreversible); | 
|---|
| 245 | libc_hidden_proto (__gconv_transliterate) | 
|---|
| 246 |  | 
|---|
| 247 | /* If NAME is an codeset alias expand it.  */ | 
|---|
| 248 | extern int __gconv_compare_alias (const char *name1, const char *name2) | 
|---|
| 249 | attribute_hidden; | 
|---|
| 250 |  | 
|---|
| 251 |  | 
|---|
| 252 | /* Builtin transformations.  */ | 
|---|
| 253 | #ifdef _LIBC | 
|---|
| 254 | # define __BUILTIN_TRANSFORM(Name) \ | 
|---|
| 255 | extern int Name (struct __gconv_step *step,				      \ | 
|---|
| 256 | struct __gconv_step_data *data,			      \ | 
|---|
| 257 | const unsigned char **inbuf,				      \ | 
|---|
| 258 | const unsigned char *inbufend,			      \ | 
|---|
| 259 | unsigned char **outbufstart, size_t *irreversible,	      \ | 
|---|
| 260 | int do_flush, int consume_incomplete) | 
|---|
| 261 |  | 
|---|
| 262 | __BUILTIN_TRANSFORM (__gconv_transform_ascii_internal); | 
|---|
| 263 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ascii); | 
|---|
| 264 | __BUILTIN_TRANSFORM (__gconv_transform_utf8_internal); | 
|---|
| 265 | __BUILTIN_TRANSFORM (__gconv_transform_internal_utf8); | 
|---|
| 266 | __BUILTIN_TRANSFORM (__gconv_transform_ucs2_internal); | 
|---|
| 267 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2); | 
|---|
| 268 | __BUILTIN_TRANSFORM (__gconv_transform_ucs2reverse_internal); | 
|---|
| 269 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2reverse); | 
|---|
| 270 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4); | 
|---|
| 271 | __BUILTIN_TRANSFORM (__gconv_transform_ucs4_internal); | 
|---|
| 272 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4le); | 
|---|
| 273 | __BUILTIN_TRANSFORM (__gconv_transform_ucs4le_internal); | 
|---|
| 274 | __BUILTIN_TRANSFORM (__gconv_transform_internal_utf16); | 
|---|
| 275 | __BUILTIN_TRANSFORM (__gconv_transform_utf16_internal); | 
|---|
| 276 | # undef __BUITLIN_TRANSFORM | 
|---|
| 277 |  | 
|---|
| 278 | /* Specialized conversion function for a single byte to INTERNAL, recognizing | 
|---|
| 279 | only ASCII characters.  */ | 
|---|
| 280 | extern wint_t __gconv_btwoc_ascii (struct __gconv_step *step, unsigned char c); | 
|---|
| 281 |  | 
|---|
| 282 | #endif | 
|---|
| 283 |  | 
|---|
| 284 | __END_DECLS | 
|---|
| 285 |  | 
|---|
| 286 | #endif /* gconv_int.h */ | 
|---|
| 287 |  | 
|---|