Back to index

glibc  2.9
8bit-gap.c
Go to the documentation of this file.
00001 /* Generic conversion to and from 8bit charsets,
00002    converting from UCS using gaps.
00003    Copyright (C) 1997-1999, 2000-2002 Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include <dlfcn.h>
00023 #include <stdint.h>
00024 
00025 struct gap
00026 {
00027   uint16_t start;
00028   uint16_t end;
00029   int32_t idx;
00030 };
00031 
00032 /* Now we can include the tables.  */
00033 #include TABLES
00034 
00035 #ifndef NONNUL
00036 # define NONNUL(c)   ((c) != '\0')
00037 #endif
00038 
00039 
00040 #define FROM_LOOP           from_gap
00041 #define TO_LOOP                    to_gap
00042 #define DEFINE_INIT         1
00043 #define DEFINE_FINI         1
00044 #define MIN_NEEDED_FROM            1
00045 #define MIN_NEEDED_TO              4
00046 
00047 
00048 /* First define the conversion function from the 8bit charset to UCS4.  */
00049 #define MIN_NEEDED_INPUT    MIN_NEEDED_FROM
00050 #define MIN_NEEDED_OUTPUT   MIN_NEEDED_TO
00051 #define LOOPFCT                    FROM_LOOP
00052 #define BODY \
00053   {                                                                  \
00054     uint32_t ch = to_ucs4[*inptr];                                   \
00055                                                                      \
00056     if (HAS_HOLES && __builtin_expect (ch == L'\0', 0) && NONNUL (*inptr))    \
00057       {                                                                     \
00058        /* This is an illegal character.  */                                 \
00059        STANDARD_FROM_LOOP_ERR_HANDLER (1);                                  \
00060       }                                                                     \
00061     else                                                             \
00062       {                                                                     \
00063        put32 (outptr, ch);                                           \
00064        outptr += 4;                                                  \
00065       }                                                                     \
00066                                                                      \
00067     ++inptr;                                                         \
00068   }
00069 #define LOOP_NEED_FLAGS
00070 #define ONEBYTE_BODY \
00071   {                                                                  \
00072     uint32_t ch = to_ucs4[c];                                               \
00073                                                                      \
00074     if (HAS_HOLES && __builtin_expect (ch == L'\0', 0) && NONNUL (c))       \
00075       return WEOF;                                                   \
00076     else                                                             \
00077       return ch;                                                     \
00078   }
00079 #include <iconv/loop.c>
00080 
00081 
00082 /* Next, define the other direction.  */
00083 #define MIN_NEEDED_INPUT    MIN_NEEDED_TO
00084 #define MIN_NEEDED_OUTPUT   MIN_NEEDED_FROM
00085 #define LOOPFCT                    TO_LOOP
00086 #define BODY \
00087   {                                                                  \
00088     const struct gap *rp = from_idx;                                        \
00089     uint32_t ch = get32 (inptr);                                     \
00090     unsigned char res;                                                      \
00091                                                                      \
00092     if (__builtin_expect (ch >= 0xffff, 0))                                 \
00093       {                                                                     \
00094        UNICODE_TAG_HANDLER (ch, 4);                                         \
00095        rp = NULL;                                                    \
00096       }                                                                     \
00097     else                                                             \
00098       while (ch > rp->end)                                           \
00099        ++rp;                                                         \
00100     if (__builtin_expect (rp == NULL, 0)                             \
00101        || __builtin_expect (ch < rp->start, 0)                              \
00102        || (res = from_ucs4[ch + rp->idx],                            \
00103            __builtin_expect (res, '\1') == '\0' && ch != 0))                \
00104       {                                                                     \
00105        /* This is an illegal character.  */                                 \
00106        STANDARD_TO_LOOP_ERR_HANDLER (4);                             \
00107       }                                                                     \
00108                                                                      \
00109     *outptr++ = res;                                                 \
00110     inptr += 4;                                                             \
00111   }
00112 #define LOOP_NEED_FLAGS
00113 #include <iconv/loop.c>
00114 
00115 
00116 /* Now define the toplevel functions.  */
00117 #include <iconv/skeleton.c>