Back to index

glibc  2.9
gconv_int.h
Go to the documentation of this file.
00001 /* Copyright (C) 1997-2005, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef _GCONV_INT_H
00021 #define _GCONV_INT_H 1
00022 
00023 #include "gconv.h"
00024 #include <stdlib.h>         /* For alloca used in macro below.  */
00025 #include <ctype.h>          /* For __toupper_l used in macro below.  */
00026 #include <string.h>         /* For strlen et al used in macro below.  */
00027 #include <bits/libc-lock.h>
00028 
00029 __BEGIN_DECLS
00030 
00031 
00032 /* Type to represent search path.  */
00033 struct path_elem
00034 {
00035   const char *name;
00036   size_t len;
00037 };
00038 
00039 /* Variable with search path for `gconv' implementation.  */
00040 extern struct path_elem *__gconv_path_elem attribute_hidden;
00041 /* Maximum length of a single path element.  */
00042 extern size_t __gconv_max_path_elem_len attribute_hidden;
00043 
00044 
00045 /* Structure for alias definition.  Simply two strings.  */
00046 struct gconv_alias
00047 {
00048   char *fromname;
00049   char *toname;
00050 };
00051 
00052 
00053 /* How many character should be conveted in one call?  */
00054 #define GCONV_NCHAR_GOAL    8160
00055 
00056 
00057 /* Structure describing one loaded shared object.  This normally are
00058    objects to perform conversation but as a special case the db shared
00059    object is also handled.  */
00060 struct __gconv_loaded_object
00061 {
00062   /* Name of the object.  It must be the first structure element.  */
00063   const char *name;
00064 
00065   /* Reference counter for the db functionality.  If no conversion is
00066      needed we unload the db library.  */
00067   int counter;
00068 
00069   /* The handle for the shared object.  */
00070   void *handle;
00071 
00072   /* Pointer to the functions the module defines.  */
00073   __gconv_fct fct;
00074   __gconv_init_fct init_fct;
00075   __gconv_end_fct end_fct;
00076 };
00077 
00078 
00079 /* Description for an available conversion module.  */
00080 struct gconv_module
00081 {
00082   const char *from_string;
00083   const char *to_string;
00084 
00085   int cost_hi;
00086   int cost_lo;
00087 
00088   const char *module_name;
00089 
00090   struct gconv_module *left;       /* Prefix smaller.  */
00091   struct gconv_module *same;       /* List of entries with identical prefix.  */
00092   struct gconv_module *right;      /* Prefix larger.  */
00093 };
00094 
00095 
00096 /* Internal data structure to represent transliteration module.  */
00097 struct trans_struct
00098 {
00099   const char *name;
00100   struct trans_struct *next;
00101 
00102   const char **csnames;
00103   size_t ncsnames;
00104   __gconv_trans_fct trans_fct;
00105   __gconv_trans_context_fct trans_context_fct;
00106   __gconv_trans_init_fct trans_init_fct;
00107   __gconv_trans_end_fct trans_end_fct;
00108 };
00109 
00110 
00111 /* Flags for `gconv_open'.  */
00112 enum
00113 {
00114   GCONV_AVOID_NOCONV = 1 << 0
00115 };
00116 
00117 /* When GCONV_AVOID_NOCONV is set and no conversion is needed,
00118    __GCONV_NULCONV should be returned.  */
00119 enum
00120 {
00121   __GCONV_NULCONV = -1
00122 };
00123 
00124 /* Global variables.  */
00125 
00126 /* Database of alias names.  */
00127 extern void *__gconv_alias_db attribute_hidden;
00128 
00129 /* Array with available modules.  */
00130 extern size_t __gconv_nmodules;
00131 extern struct gconv_module *__gconv_modules_db attribute_hidden;
00132 
00133 /* Value of the GCONV_PATH environment variable.  */
00134 extern const char *__gconv_path_envvar attribute_hidden;
00135 
00136 /* Lock for the conversion database content.  */
00137 __libc_lock_define (extern, __gconv_lock attribute_hidden)
00138 
00139 
00140 /* The gconv functions expects the name to be in upper case and complete,
00141    including the trailing slashes if necessary.  */
00142 #define norm_add_slashes(str,suffix) \
00143   ({                                                                 \
00144     const char *cp = (str);                                          \
00145     char *result;                                                    \
00146     char *tmp;                                                              \
00147     size_t cnt = 0;                                                  \
00148     const size_t suffix_len = strlen (suffix);                              \
00149                                                                      \
00150     while (*cp != '\0')                                                     \
00151       if (*cp++ == '/')                                                     \
00152        ++cnt;                                                        \
00153                                                                      \
00154     tmp = result = __alloca (cp - (str) + 3 + suffix_len);                  \
00155     cp = (str);                                                             \
00156     while (*cp != '\0')                                                     \
00157       *tmp++ = __toupper_l (*cp++, _nl_C_locobj_ptr);                       \
00158     if (cnt < 2)                                                     \
00159       {                                                                     \
00160        *tmp++ = '/';                                                 \
00161        if (cnt < 1)                                                  \
00162          {                                                           \
00163            *tmp++ = '/';                                             \
00164            if (suffix_len != 0)                                      \
00165              tmp = __mempcpy (tmp, suffix, suffix_len);              \
00166          }                                                           \
00167       }                                                                     \
00168     *tmp = '\0';                                                     \
00169     result;                                                          \
00170   })
00171 
00172 
00173 /* Return in *HANDLE decriptor for transformation from FROMSET to TOSET.  */
00174 extern int __gconv_open (const char *toset, const char *fromset,
00175                       __gconv_t *handle, int flags)
00176      internal_function;
00177 
00178 /* Free resources associated with transformation descriptor CD.  */
00179 extern int __gconv_close (__gconv_t cd)
00180      internal_function;
00181 
00182 /* Transform at most *INBYTESLEFT bytes from buffer starting at *INBUF
00183    according to rules described by CD and place up to *OUTBYTESLEFT
00184    bytes in buffer starting at *OUTBUF.  Return number of non-identical
00185    conversions in *IRREVERSIBLE if this pointer is not null.  */
00186 extern int __gconv (__gconv_t cd, const unsigned char **inbuf,
00187                   const unsigned char *inbufend, unsigned char **outbuf,
00188                   unsigned char *outbufend, size_t *irreversible)
00189      internal_function;
00190 
00191 /* Return in *HANDLE a pointer to an array with *NSTEPS elements describing
00192    the single steps necessary for transformation from FROMSET to TOSET.  */
00193 extern int __gconv_find_transform (const char *toset, const char *fromset,
00194                                struct __gconv_step **handle,
00195                                size_t *nsteps, int flags)
00196      internal_function;
00197 
00198 /* Search for transformation in cache data.  */
00199 extern int __gconv_lookup_cache (const char *toset, const char *fromset,
00200                              struct __gconv_step **handle, size_t *nsteps,
00201                              int flags)
00202      internal_function;
00203 
00204 /* Compare the two name for whether they are after alias expansion the
00205    same.  This function uses the cache and fails if none is
00206    loaded.  */
00207 extern int __gconv_compare_alias_cache (const char *name1, const char *name2,
00208                                    int *result) internal_function;
00209 
00210 /* Free data associated with a step's structure.  */
00211 extern void __gconv_release_step (struct __gconv_step *step)
00212      internal_function;
00213 
00214 /* Read all the configuration data and cache it.  */
00215 extern void __gconv_read_conf (void) attribute_hidden;
00216 
00217 /* Try to read module cache file.  */
00218 extern int __gconv_load_cache (void) internal_function;
00219 
00220 /* Retrieve pointer to internal cache.  */
00221 extern void *__gconv_get_cache (void);
00222 
00223 /* Retrieve pointer to internal module database.  */
00224 extern struct gconv_module *__gconv_get_modules_db (void);
00225 
00226 /* Retrieve pointer to internal alias database.  */
00227 extern void *__gconv_get_alias_db (void);
00228 
00229 /* Determine the directories we are looking in.  */
00230 extern void __gconv_get_path (void) internal_function;
00231 
00232 /* Comparison function to search alias.  */
00233 extern int __gconv_alias_compare (const void *p1, const void *p2)
00234      attribute_hidden;
00235 
00236 /* Clear reference to transformation step implementations which might
00237    cause the code to be unloaded.  */
00238 extern int __gconv_close_transform (struct __gconv_step *steps,
00239                                 size_t nsteps)
00240      internal_function;
00241 
00242 /* Free all resources allocated for the transformation record when
00243    using the cache.  */
00244 extern void __gconv_release_cache (struct __gconv_step *steps, size_t nsteps)
00245      internal_function;
00246 
00247 /* Load shared object named by NAME.  If already loaded increment reference
00248    count.  */
00249 extern struct __gconv_loaded_object *__gconv_find_shlib (const char *name)
00250      internal_function;
00251 
00252 /* Release shared object.  If no further reference is available unload
00253    the object.  */
00254 extern void __gconv_release_shlib (struct __gconv_loaded_object *handle)
00255      internal_function;
00256 
00257 /* Fill STEP with information about builtin module with NAME.  */
00258 extern void __gconv_get_builtin_trans (const char *name,
00259                                    struct __gconv_step *step)
00260      internal_function;
00261 
00262 /* Try to load transliteration step module.  */
00263 extern int __gconv_translit_find (struct trans_struct *trans)
00264      internal_function;
00265 
00266 /* Transliteration using the locale's data.  */
00267 extern int __gconv_transliterate (struct __gconv_step *step,
00268                               struct __gconv_step_data *step_data,
00269                               void *trans_data,
00270                               __const unsigned char *inbufstart,
00271                               __const unsigned char **inbufp,
00272                               __const unsigned char *inbufend,
00273                               unsigned char **outbufstart,
00274                               size_t *irreversible) attribute_hidden;
00275 
00276 
00277 /* If NAME is an codeset alias expand it.  */
00278 extern int __gconv_compare_alias (const char *name1, const char *name2)
00279      internal_function;
00280 
00281 
00282 /* Builtin transformations.  */
00283 #ifdef _LIBC
00284 # define __BUILTIN_TRANSFORM(Name) \
00285   extern int Name (struct __gconv_step *step,                               \
00286                  struct __gconv_step_data *data,                     \
00287                  const unsigned char **inbuf,                               \
00288                  const unsigned char *inbufend,                      \
00289                  unsigned char **outbufstart, size_t *irreversible,         \
00290                  int do_flush, int consume_incomplete)
00291 
00292 __BUILTIN_TRANSFORM (__gconv_transform_ascii_internal);
00293 __BUILTIN_TRANSFORM (__gconv_transform_internal_ascii);
00294 __BUILTIN_TRANSFORM (__gconv_transform_utf8_internal);
00295 __BUILTIN_TRANSFORM (__gconv_transform_internal_utf8);
00296 __BUILTIN_TRANSFORM (__gconv_transform_ucs2_internal);
00297 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2);
00298 __BUILTIN_TRANSFORM (__gconv_transform_ucs2reverse_internal);
00299 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2reverse);
00300 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4);
00301 __BUILTIN_TRANSFORM (__gconv_transform_ucs4_internal);
00302 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4le);
00303 __BUILTIN_TRANSFORM (__gconv_transform_ucs4le_internal);
00304 __BUILTIN_TRANSFORM (__gconv_transform_internal_utf16);
00305 __BUILTIN_TRANSFORM (__gconv_transform_utf16_internal);
00306 # undef __BUITLIN_TRANSFORM
00307 
00308 /* Specialized conversion function for a single byte to INTERNAL, recognizing
00309    only ASCII characters.  */
00310 extern wint_t __gconv_btwoc_ascii (struct __gconv_step *step, unsigned char c);
00311 
00312 #endif
00313 
00314 __END_DECLS
00315 
00316 #endif /* gconv_int.h */