lib/iconv.h

00001 /*-
00002  * Copyright (c) 1999,2000
00003  *      Konstantin Chuguev.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. All advertising materials mentioning features or use of this software
00014  *    must display the following acknowledgement:
00015  *      This product includes software developed by Konstantin Chuguev
00016  *      and its contributors.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00022  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00024  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00025  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00027  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  */
00031 
00032 #ifndef _ICONV_H_
00033 #define _ICONV_H_
00034 
00035 #include "apr.h"
00036 #include "apr_pools.h"
00037 #include "apr_iconv.h"
00038 #ifdef WIN32
00039 #define ICONV_DEFAULT_PATH "iconv"
00040 #define API_USE_BUILTIN_ALIASES /* Use the built-in charset alias table */
00041 #else
00042 #include "apr_iconv_private.h"  /* contains ICONV_DEFAULT_PATH */
00043 #ifdef _OSD_POSIX
00044 #define API_USE_BUILTIN_ALIASES /* Use the built-in charset alias table */
00045 #endif
00046 #endif
00047 
00048 #include <stddef.h>
00049 
00050 #ifdef ICONV_INTERNAL
00051 
00052 /*
00053  * iconv module types
00054  */
00055 #define ICMOD_ANY       0
00056 #define ICMOD_LIB       1
00057 #define ICMOD_UC_CCS    0x100   /* UC - CCS for CES */
00058 #define ICMOD_UC_CES    0x101   /* UC - CES */
00059 
00060 /*
00061  * iconv module flags
00062  */
00063 #define ICMODF_LOADED   0x8000
00064 
00065 /*
00066  * iconv module handler events
00067  */
00068 #define ICMODEV_LOAD    1       /* module load. after dependencies resolved */
00069 #define ICMODEV_UNLOAD  2       /* module unload */
00070 #define ICMODEV_DYN_LOAD    3   /* load dynamic dependencies */
00071 #define ICMODEV_DYN_UNLOAD  4   /* unload dynamic dependencies */
00072 
00073 struct iconv_module_depend {
00074         int             md_type;
00075         const char *    md_name;
00076         const void *    md_data;
00077 };
00078 
00079 struct iconv_module;
00080 
00081 /* _tbl_simple.c table_load_ccs() calls apr_iconv_mod_load(...ctx) */
00082 
00083 typedef int iconv_mod_event_t(struct iconv_module *, int, apr_pool_t *ctx);
00084 
00085 struct iconv_module_desc {
00086         int             imd_type;
00087         iconv_mod_event_t *imd_event;
00088         const struct iconv_module_depend *imd_depend;
00089         const void *    imd_data;
00090 };
00091 
00092 #define END_ICONV_MODULE_DEPEND  {0, NULL, NULL}
00093 
00094 #define ICONV_MODULE(type,data) struct iconv_module_desc iconv_module \
00095                                     {(type), (data)}
00096 
00097 struct iconv_module {
00098         int             im_flags;
00099         void *          im_handle;
00100         struct iconv_module_desc *im_desc;
00101         struct iconv_module *im_next;
00102         struct iconv_module *im_deplist;
00103         int             im_depcnt;
00104         const void *    im_methods;
00105         void *          im_data;
00106         const void *    im_depdata;     /* data if module loaded from dependency */
00107         const void *    im_args;
00108 
00109         /* This is module-private data. Nothing outside the module
00110            itself may touch it. */
00111         void *im_private;
00112 };
00113 
00114 #define ICONV_MOD_LOAD(mod,ctx) (mod)->im_desc->imd_event(mod, ICMODEV_LOAD,ctx)
00115 #define ICONV_MOD_UNLOAD(mod,ctx)       (mod)->im_desc->imd_event(mod, ICMODEV_UNLOAD,ctx)
00116 #define ICONV_MOD_DYN_LOAD(mod,ctx)     (mod)->im_desc->imd_event(mod, ICMODEV_DYN_LOAD,ctx)
00117 #define ICONV_MOD_DYN_UNLOAD(mod,ctx)   (mod)->im_desc->imd_event(mod, ICMODEV_DYN_UNLOAD,ctx)
00118 
00119 /*
00120  * iconv converter definitions.
00121  */
00122 typedef int iconv_open_t(const char *, const char *, void **, apr_pool_t *);
00123 typedef int iconv_close_t(void *, apr_pool_t *);
00124 typedef apr_status_t iconv_conv_t(void *, const unsigned char **, apr_size_t *,
00125         unsigned char **, apr_size_t *, apr_size_t *);
00126 
00127 struct iconv_converter_desc {
00128         iconv_open_t *  icd_open;
00129         iconv_close_t * icd_close;
00130         iconv_conv_t *  icd_conv;
00131 };
00132 
00133 struct iconv_converter {
00134         struct iconv_converter_desc *ic_desc;
00135         void *          ic_data;
00136 };
00137 
00138 /*
00139  * internal data types and functions used by charset conversion modules
00140  */
00141 
00142 #define UCS_CHAR_ZERO_WIDTH_NBSP 0xFEFF
00143 #define UCS_CHAR_INVALID         0xFFFE
00144 #define UCS_CHAR_NONE            0xFFFF
00145 
00146 typedef apr_uint16_t ucs2_t;    /* Unicode character [D5] */
00147 typedef apr_uint32_t ucs4_t;    /* Unicode scalar character [D28] */
00148 #define ucs_t    ucs4_t
00149 
00150 /*
00151  * one-level coded character set conversion tables
00152  */
00153 typedef struct {
00154         apr_uint16_t    data[128];
00155 } iconv_ccs_convtable_7bit;     /* 7-bit charset to Unicode */
00156 
00157 typedef struct {
00158         apr_uint16_t    data[256];
00159 } iconv_ccs_convtable_8bit;     /* 8-bit charset to Unicode */
00160 
00161 /*
00162  * two-level coded character set conversion tables
00163  */
00164 typedef struct {
00165         const iconv_ccs_convtable_7bit  *data[128];
00166 } iconv_ccs_convtable_14bit;    /* 14-bit charset to Unicode */
00167 
00168 typedef struct {
00169         const iconv_ccs_convtable_8bit  *data[256];
00170 } iconv_ccs_convtable_16bit;    /* 16-bit charset to Unicode;
00171                                  * Unicode to any charset */
00172 
00173 /*
00174  * abstract coded character set conversion table
00175  */
00176 typedef union {
00177         iconv_ccs_convtable_7bit        _7bit;
00178         iconv_ccs_convtable_8bit        _8bit;
00179         iconv_ccs_convtable_14bit       _14bit;
00180         iconv_ccs_convtable_16bit       _16bit;
00181 } iconv_ccs_convtable;
00182 
00183 /*
00184  * function for table-driven conversion
00185  */
00186 typedef ucs2_t iconv_ccs_converter_t (ucs2_t ch);
00187 
00188 /*
00189  * charset conversion module descriptor
00190  */
00191 struct iconv_ccs_desc {
00192         const char * const *            names;
00193         int                             nbits;
00194         const iconv_ccs_convtable *     from_ucs;
00195         const iconv_ccs_convtable *     to_ucs;
00196         iconv_ccs_converter_t *         convert_from_ucs;
00197         iconv_ccs_converter_t *         convert_to_ucs;
00198 };
00199 
00200 /*
00201  * inline functions for use in charset conversion modules
00202  */
00203 static APR_INLINE ucs2_t
00204 iconv_ccs_convert_7bit(const iconv_ccs_convtable *table, ucs2_t ch)
00205 {
00206         return ch & 0x80 ? UCS_CHAR_INVALID : table->_7bit.data[ch];
00207 }
00208 
00209 static APR_INLINE ucs2_t
00210 iconv_ccs_convert_8bit(const iconv_ccs_convtable *table, ucs2_t ch)
00211 {
00212         return table->_8bit.data[ch];
00213 }
00214 
00215 static APR_INLINE ucs2_t
00216 iconv_ccs_convert_14bit(const iconv_ccs_convtable *table, ucs2_t ch)
00217 {
00218         const iconv_ccs_convtable_7bit *sub_table;
00219 
00220         sub_table =  ch & 0x8080 ? NULL : table->_14bit.data[ch >> 8];
00221         return sub_table ? sub_table->data[ch & 0x7F] : UCS_CHAR_INVALID;
00222 }
00223 
00224 static APR_INLINE ucs2_t
00225 iconv_ccs_convert_16bit(const iconv_ccs_convtable *table, ucs2_t ch)
00226 {
00227         const iconv_ccs_convtable_8bit *sub_table;
00228 
00229         sub_table = table->_16bit.data[ch >> 8];
00230         return sub_table ? sub_table->data[ch & 0xFF] : UCS_CHAR_INVALID;
00231 }
00232 
00233 #define ICONV_CCS_CONVERT_FROM_UCS(ccsd, ch) \
00234         ((ccsd)->convert_from_ucs(ch))
00235 #define ICONV_CCS_CONVERT_TO_UCS(ccsd, ch) \
00236         ((ccsd)->convert_to_ucs(ch))
00237 
00238 /*
00239  * generic character encoding schemes and functions
00240  */
00241 struct iconv_ces;
00242 
00243 typedef int  iconv_ces_open_t(struct iconv_ces *, apr_pool_t *);
00244 typedef int  iconv_ces_close_t(struct iconv_ces *);
00245 typedef void iconv_ces_reset_t(struct iconv_ces *);
00246 typedef const char * const *iconv_ces_names_t(struct iconv_ces *);
00247 typedef int  iconv_ces_nbits_t(struct iconv_ces *);
00248 typedef int  iconv_ces_nbytes_t(struct iconv_ces *);
00249 
00250 typedef apr_ssize_t iconv_ces_convert_from_ucs_t
00251     (struct iconv_ces *data, ucs_t in,
00252     unsigned char **outbuf, apr_size_t *outbytesleft);
00253 
00254 typedef ucs_t iconv_ces_convert_to_ucs_t
00255     (struct iconv_ces *data,
00256     const unsigned char **inbuf, apr_size_t *inbytesleft);
00257 
00258 struct iconv_ces_desc {
00259         iconv_ces_open_t *      open;
00260         iconv_ces_close_t *     close;
00261         iconv_ces_reset_t *     reset;
00262         iconv_ces_names_t *     names;
00263         iconv_ces_nbits_t *     nbits;
00264         iconv_ces_nbytes_t *    nbytes;
00265         iconv_ces_convert_from_ucs_t * convert_from_ucs;
00266         iconv_ces_convert_to_ucs_t * convert_to_ucs;
00267         const void *data;
00268 };
00269 
00270 struct iconv_ces {
00271         struct iconv_ces_desc * desc;
00272         void *                  data;
00273         struct iconv_module *   mod;
00274 };
00275 
00276 API_DECLARE_NONSTD(int)  apr_iconv_ces_open(const char *ces_name, struct iconv_ces **cespp, apr_pool_t *ctx);
00277 API_DECLARE_NONSTD(int)  apr_iconv_ces_close(struct iconv_ces *ces, apr_pool_t *ctx);
00278 API_DECLARE_NONSTD(int)  apr_iconv_ces_open_func(struct iconv_ces *ces, apr_pool_t *ctx);
00279 API_DECLARE_NONSTD(int)  apr_iconv_ces_close_func(struct iconv_ces *ces);
00280 API_DECLARE_NONSTD(void) apr_iconv_ces_reset_func(struct iconv_ces *ces);
00281 API_DECLARE_NONSTD(void) apr_iconv_ces_no_func(struct iconv_ces *ces);
00282 API_DECLARE_NONSTD(int)  apr_iconv_ces_nbits7(struct iconv_ces *ces);
00283 API_DECLARE_NONSTD(int)  apr_iconv_ces_nbits8(struct iconv_ces *ces);
00284 API_DECLARE_NONSTD(int)  apr_iconv_ces_zero(struct iconv_ces *ces);
00285 
00286 /* Safe for _NONSTD APIs since the C caller pops the stack and the
00287  * apr_pool_t *cxt arg is simply ignored:
00288  */
00289 #define apr_iconv_ces_open_zero (iconv_ces_open_t*)apr_iconv_ces_zero
00290 
00291 #define iconv_char32bit(ch)     ((ch) & 0xFFFF0000)
00292 
00293 #define ICONV_CES_OPEN(ces,ctx) (ces)->desc->open(ces,ctx)
00294 #define ICONV_CES_CLOSE(ces)    (ces)->desc->close(ces)
00295 #define ICONV_CES_RESET(ces)    (ces)->desc->reset(ces)
00296 #define ICONV_CES_CONVERT_FROM_UCS(cesd, in, outbuf, outbytes) \
00297             ((cesd)->desc->convert_from_ucs((cesd), (in), (outbuf), (outbytes)))
00298 #define ICONV_CES_CONVERT_TO_UCS(cesd, inbuf, inbytes) \
00299             ((cesd)->desc->convert_to_ucs((cesd), (inbuf), (inbytes)))
00300 
00301 #define ICONV_CES_DRIVER_DECL(name) \
00302                 iconv_ces_open_t iconv_##name##_open; \
00303                 iconv_ces_close_t iconv_##name##_close; \
00304                 iconv_ces_reset_t iconv_##name##_reset; \
00305                 iconv_ces_nbits_t iconv_##name##_nbits; \
00306                 iconv_ces_convert_from_ucs_t iconv_##name##_convert_from_ucs; \
00307                 iconv_ces_convert_to_ucs_t iconv_##name##_convert_to_ucs
00308 
00309 /*
00310  ************************************************
00311  * EUC character encoding schemes and functions *
00312  ************************************************
00313  */
00314 
00315 typedef struct iconv_ces_euc_ccs {
00316         const char      *prefix;
00317         apr_size_t              prefixlen;
00318 } iconv_ces_euc_ccs_t;
00319 
00320 ICONV_CES_DRIVER_DECL(euc);
00321 
00322 /*
00323  * ISO-2022 character encoding schemes and functions
00324  */
00325 enum { ICONV_SHIFT_SI = 0, ICONV_SHIFT_SO, ICONV_SHIFT_SS2, ICONV_SHIFT_SS3 };
00326 
00327 typedef struct iconv_ces_iso2022_ccs {
00328         int             shift;
00329         const char *    designator;
00330         apr_size_t              designatorlen;
00331 } iconv_ces_iso2022_ccs_t;
00332 
00333 typedef struct {
00334         const int *     shift_tab;
00335         int             nccs;
00336         const struct iconv_ces_iso2022_ccs *ccs;
00337 } iconv_ces_iso2022_data;
00338 
00339 ICONV_CES_DRIVER_DECL(iso2022);
00340 
00341 
00342 API_DECLARE_NONSTD(int) apr_iconv_mod_load(const char *, int, const void *, struct iconv_module **, apr_pool_t *);
00343 API_DECLARE_NONSTD(int) apr_iconv_mod_unload(struct iconv_module *,apr_pool_t *ctx);
00344 API_DECLARE_NONSTD(int) apr_iconv_mod_noevent(struct iconv_module *mod, int event, apr_pool_t *ctx);
00345 API_DECLARE_NONSTD(int) apr_iconv_ccs_event(struct iconv_module *mod, int event, apr_pool_t *ctx);
00346 
00347 int  iconv_malloc(apr_size_t size, void **pp);
00348 
00349 extern struct iconv_converter_desc iconv_uc_desc;
00350 
00351 
00352 API_DECLARE_NONSTD(apr_status_t) apr_iconv_euc_open(struct iconv_ces *ces, apr_pool_t *ctx);
00353 API_DECLARE_NONSTD(apr_status_t) apr_iconv_euc_close(struct iconv_ces *ces);
00354 API_DECLARE_NONSTD(apr_ssize_t)  apr_iconv_euc_convert_from_ucs(struct iconv_ces *ces, ucs_t in,        unsigned char **outbuf, apr_size_t *outbytesleft);
00355 API_DECLARE_NONSTD(ucs_t)  apr_iconv_euc_convert_to_ucs(struct iconv_ces *ces, const unsigned char **inbuf, apr_size_t *inbytesleft);
00356 
00357 API_DECLARE_NONSTD(apr_status_t) apr_iconv_iso2022_open(struct iconv_ces *ces, apr_pool_t *ctx);
00358 API_DECLARE_NONSTD(int) apr_iconv_iso2022_close(struct iconv_ces *ces);
00359 API_DECLARE_NONSTD(void) apr_iconv_iso2022_reset(struct iconv_ces *ces);
00360 API_DECLARE_NONSTD(apr_ssize_t) apr_iconv_iso2022_convert_from_ucs(struct iconv_ces *ces, ucs_t in, unsigned char **outbuf, apr_size_t *outbytesleft);
00361 API_DECLARE_NONSTD(ucs_t) apr_iconv_iso2022_convert_to_ucs(struct iconv_ces *ces, const unsigned char **inbuf, apr_size_t *inbytesleft);
00362 
00363 #endif /* ICONV_INTERNAL */
00364 
00365 #endif /* _ICONV_H_ */

Generated on Tue Nov 27 09:53:45 2007 for Apache Portable Runtime Iconv Library by  doxygen 1.5.2