(linenum→info "unix/slp.c:2238")

glibc/2.7/iconv/gconv_open.c

    1: /* Find matching transformation algorithms and initialize steps.
    2:    Copyright (C) 1997,1998,1999,2000,2001,2004,2005,2007
    3:         Free Software Foundation, Inc.
    4:    This file is part of the GNU C Library.
    5:    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
    6: 
    7:    The GNU C Library is free software; you can redistribute it and/or
    8:    modify it under the terms of the GNU Lesser General Public
    9:    License as published by the Free Software Foundation; either
   10:    version 2.1 of the License, or (at your option) any later version.
   11: 
   12:    The GNU C Library is distributed in the hope that it will be useful,
   13:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   14:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15:    Lesser General Public License for more details.
   16: 
   17:    You should have received a copy of the GNU Lesser General Public
   18:    License along with the GNU C Library; if not, write to the Free
   19:    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   20:    02111-1307 USA.  */
   21: 
   22: #include <errno.h>
   23: #include <locale.h>
   24: #include "../locale/localeinfo.h"
   25: #include <stdlib.h>
   26: #include <string.h>
   27: 
   28: #include <gconv_int.h>
   29: 
   30: 
   31: int
   32: internal_function
   33: __gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
   34:               int flags)
   35: {
   36:   struct __gconv_step *steps;
   37:   size_t nsteps;
   38:   __gconv_t result = NULL;
   39:   size_t cnt = 0;
   40:   int res;
   41:   int conv_flags = 0;
   42:   const char *errhand;
   43:   const char *ignore;
   44:   struct trans_struct *trans = NULL;
   45: 
   46:   /* Find out whether any error handling method is specified.  */
   47:   errhand = strchr (toset, '/');
   48:   if (errhand != NULL)
   49:     errhand = strchr (errhand + 1, '/');
   50:   if (__builtin_expect (errhand != NULL, 1))
   51:     {
   52:       if (*++errhand == '\0')
   53:         errhand = NULL;
   54:       else
   55:         {
   56:           /* Make copy without the error handling description.  */
   57:           char *newtoset = (char *) alloca (errhand - toset + 1);
   58:           char *tok;
   59:           char *ptr = NULL /* Work around a bogus warning */;
   60: 
   61:           newtoset[errhand - toset] = '\0';
   62:           toset = memcpy (newtoset, toset, errhand - toset);
   63: 
   64:           /* Find the appropriate transliteration handlers.  */
   65:           tok = strdupa (errhand);
   66: 
   67:           tok = __strtok_r (tok, ",", &ptr);
   68:           while (tok != NULL)
   69:             {
   70:               if (__strcasecmp_l (tok, "TRANSLIT", _nl_C_locobj_ptr) == 0)
   71:                 {
   72:                   /* It's the builtin transliteration handling.  We only
   73:                      support it for working on the internal encoding.  */
   74:                   static const char *const internal_trans_names[1]
   75:                     = { "INTERNAL" };
   76:                   struct trans_struct *lastp = NULL;
   77:                   struct trans_struct *runp;
   78: 
   79:                   for (runp = trans; runp != NULL; runp = runp->next)
   80:                     if (runp->trans_fct == __gconv_transliterate)
   81:                       break;
   82:                     else
   83:                       lastp = runp;
   84: 
   85:                   if (runp == NULL)
   86:                     {
   87:                       struct trans_struct *newp;
   88: 
   89:                       newp = (struct trans_struct *) alloca (sizeof (*newp));
   90:                       memset (newp, '\0', sizeof (*newp));
   91: 
   92:                       /* We leave the `name' field zero to signal that
   93:                          this is an internal transliteration step.  */
   94:                       newp->csnames = (const char **) internal_trans_names;
   95:                       newp->ncsnames = 1;
   96:                       newp->trans_fct = __gconv_transliterate;
   97: 
   98:                       if (lastp == NULL)
   99:                         trans = newp;
  100:                       else
  101:                         lastp->next = newp;
  102:                     }
  103:                 }
  104:               else if (__strcasecmp_l (tok, "IGNORE", _nl_C_locobj_ptr) == 0)
  105:                 /* Set the flag to ignore all errors.  */
  106:                 conv_flags |= __GCONV_IGNORE_ERRORS;
  107:               else
  108:                 {
  109:                   /* `tok' is possibly a module name.  We'll see later
  110:                      whether we can find it.  But first see that we do
  111:                      not already a module of this name.  */
  112:                   struct trans_struct *lastp = NULL;
  113:                   struct trans_struct *runp;
  114: 
  115:                   for (runp = trans; runp != NULL; runp = runp->next)
  116:                     if (runp->name != NULL
  117:                         && __strcasecmp_l (tok, runp->name,
  118:                                            _nl_C_locobj_ptr) == 0)
  119:                       break;
  120:                     else
  121:                       lastp = runp;
  122: 
  123:                   if (runp == NULL)
  124:                     {
  125:                       struct trans_struct *newp;
  126: 
  127:                       newp = (struct trans_struct *) alloca (sizeof (*newp));
  128:                       memset (newp, '\0', sizeof (*newp));
  129:                       newp->name = tok;
  130: 
  131:                       if (lastp == NULL)
  132:                         trans = newp;
  133:                       else
  134:                         lastp->next = newp;
  135:                     }
  136:                 }
  137: 
  138:               tok = __strtok_r (NULL, ",", &ptr);
  139:             }
  140:         }
  141:     }
  142: 
  143:   /* For the source character set we ignore the error handler specification.
  144:      XXX Is this really always the best?  */
  145:   ignore = strchr (fromset, '/');
  146:   if (ignore != NULL && (ignore = strchr (ignore + 1, '/')) != NULL
  147:       && *++ignore != '\0')
  148:     {
  149:       char *newfromset = (char *) alloca (ignore - fromset + 1);
  150: 
  151:       newfromset[ignore - fromset] = '\0';
  152:       fromset = memcpy (newfromset, fromset, ignore - fromset);
  153:     }
  154: 
  155:   /* If the string is empty define this to mean the charset of the
  156:      currently selected locale.  */
  157:   if (strcmp (toset, "//") == 0)
  158:     {
  159:       const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
  160:       size_t len = strlen (codeset);
  161:       char *dest;
  162:       toset = dest = (char *) alloca (len + 3);
  163:       memcpy (__mempcpy (dest, codeset, len), "//", 3);
  164:     }
  165:   if (strcmp (fromset, "//") == 0)
  166:     {
  167:       const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
  168:       size_t len = strlen (codeset);
  169:       char *dest;
  170:       fromset = dest = (char *) alloca (len + 3);
  171:       memcpy (__mempcpy (dest, codeset, len), "//", 3);
  172:     }
  173: 
  174:   res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
  175:   if (res == __GCONV_OK)
  176:     {
  177:       /* Find the modules.  */
  178:       struct trans_struct *lastp = NULL;
  179:       struct trans_struct *runp;
  180: 
  181:       for (runp = trans; runp != NULL; runp = runp->next)
  182:         {
  183:           if (runp->name == NULL
  184:               || __builtin_expect (__gconv_translit_find (runp), 0) == 0)
  185:             lastp = runp;
  186:           else
  187:             {
  188:               /* This means we haven't found the module.  Remove it.  */
  189:               if (lastp == NULL)
  190:                 trans  = runp->next;
  191:               else
  192:                 lastp->next  = runp->next;
  193:             }
  194:         }
  195: 
  196:       /* Allocate room for handle.  */
  197:       result = (__gconv_t) malloc (sizeof (struct __gconv_info)
  198:                                    + (nsteps
  199:                                       * sizeof (struct __gconv_step_data)));
  200:       if (result == NULL)
  201:         res = __GCONV_NOMEM;
  202:       else
  203:         {
  204:           size_t n;
  205: 
  206:           /* Remember the list of steps.  */
  207:           result->__steps = steps;
  208:           result->__nsteps = nsteps;
  209: 
  210:           /* Clear the array for the step data.  */
  211:           memset (result->__data, '\0',
  212:                   nsteps * sizeof (struct __gconv_step_data));
  213: 
  214:           /* Call all initialization functions for the transformation
  215:              step implementations.  */
  216:           for (cnt = 0; cnt < nsteps; ++cnt)
  217:             {
  218:               size_t size;
  219: 
  220:               /* Would have to be done if we would not clear the whole
  221:                  array above.  */
  222: #if 0
  223:               /* Reset the counter.  */
  224:               result->__data[cnt].__invocation_counter = 0;
  225: 
  226:               /* It's a regular use.  */
  227:               result->__data[cnt].__internal_use = 0;
  228: #endif
  229: 
  230:               /* We use the `mbstate_t' member in DATA.  */
  231:               result->__data[cnt].__statep = &result->__data[cnt].__state;
  232: 
  233:               /* Now see whether we can use any of the transliteration
  234:                  modules for this step.  */
  235:               for (runp = trans; runp != NULL; runp = runp->next)
  236:                 for (n = 0; n < runp->ncsnames; ++n)
  237:                   if (__strcasecmp_l (steps[cnt].__from_name,
  238:                                       runp->csnames[n], _nl_C_locobj_ptr) == 0)
  239:                     {
  240:                       void *data = NULL;
  241: 
  242:                       /* Match!  Now try the initializer.  */
  243:                       if (runp->trans_init_fct == NULL
  244:                           || (runp->trans_init_fct (&data,
  245:                                                     steps[cnt].__to_name)
  246:                               == __GCONV_OK))
  247:                         {
  248:                           /* Append at the end of the list.  */
  249:                           struct __gconv_trans_data *newp;
  250:                           struct __gconv_trans_data **lastp;
  251: 
  252:                           newp = (struct __gconv_trans_data *)
  253:                             malloc (sizeof (struct __gconv_trans_data));
  254:                           if (newp == NULL)
  255:                             {
  256:                               res = __GCONV_NOMEM;
  257:                               goto bail;
  258:                             }
  259: 
  260:                           newp->__trans_fct = runp->trans_fct;
  261:                           newp->__trans_context_fct = runp->trans_context_fct;
  262:                           newp->__trans_end_fct = runp->trans_end_fct;
  263:                           newp->__data = data;
  264:                           newp->__next = NULL;
  265: 
  266:                           lastp = &result->__data[cnt].__trans;
  267:                           while (*lastp != NULL)
  268:                             lastp = &(*lastp)->__next;
  269: 
  270:                           *lastp = newp;
  271:                         }
  272:                       break;
  273:                     }
  274: 
  275:               /* If this is the last step we must not allocate an
  276:                  output buffer.  */
  277:               if (cnt < nsteps - 1)
  278:                 {
  279:                   result->__data[cnt].__flags = conv_flags;
  280: 
  281:                   /* Allocate the buffer.  */
  282:                   size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
  283: 
  284:                   result->__data[cnt].__outbuf = malloc (size);
  285:                   if (result->__data[cnt].__outbuf == NULL)
  286:                     {
  287:                       res = __GCONV_NOMEM;
  288:                       goto bail;
  289:                     }
  290: 
  291:                   result->__data[cnt].__outbufend =
  292:                     result->__data[cnt].__outbuf + size;
  293:                 }
  294:               else
  295:                 {
  296:                   /* Handle the last entry.  */
  297:                   result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
  298: 
  299:                   break;
  300:                 }
  301:             }
  302:         }
  303: 
  304:       if (res != __GCONV_OK)
  305:         {
  306:           /* Something went wrong.  Free all the resources.  */
  307:           int serrno;
  308:         bail:
  309:           serrno = errno;
  310: 
  311:           if (result != NULL)
  312:             {
  313:               while (cnt-- > 0)
  314:                 {
  315:                   struct __gconv_trans_data *transp;
  316: 
  317:                   transp = result->__data[cnt].__trans;
  318:                   while (transp != NULL)
  319:                     {
  320:                       struct __gconv_trans_data *curp = transp;
  321:                       transp = transp->__next;
  322: 
  323:                       if (__builtin_expect (curp->__trans_end_fct != NULL, 0))
  324:                         curp->__trans_end_fct (curp->__data);
  325: 
  326:                       free (curp);
  327:                     }
  328: 
  329:                   free (result->__data[cnt].__outbuf);
  330:                 }
  331: 
  332:               free (result);
  333:               result = NULL;
  334:             }
  335: 
  336:           __gconv_close_transform (steps, nsteps);
  337: 
  338:           __set_errno (serrno);
  339:         }
  340:     }
  341: 
  342:   *handle = result;
  343:   return res;
  344: }
Syntax (Markdown)