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 #endif
00044 
00045 #include <stddef.h>
00046 
00047 #ifdef ICONV_INTERNAL
00048 
00049 /*
00050  * iconv module types
00051  */
00052 #define ICMOD_ANY       0
00053 #define ICMOD_LIB       1
00054 #define ICMOD_UC_CCS    0x100   /* UC - CCS for CES */
00055 #define ICMOD_UC_CES    0x101   /* UC - CES */
00056 
00057 /*
00058  * iconv module flags
00059  */
00060 #define ICMODF_LOADED   0x8000
00061 
00062 /*
00063  * iconv module handler events
00064  */
00065 #define ICMODEV_LOAD    1       /* module load. after dependencies resolved */
00066 #define ICMODEV_UNLOAD  2       /* module unload */
00067 #define ICMODEV_DYN_LOAD    3   /* load dynamic dependencies */
00068 #define ICMODEV_DYN_UNLOAD  4   /* unload dynamic dependencies */
00069 
00070 struct iconv_module_depend {
00071         int             md_type;
00072         const char *    md_name;
00073         const void *    md_data;
00074 };
00075 
00076 struct iconv_module;
00077 
00078 /* _tbl_simple.c table_load_ccs() calls apr_iconv_mod_load(...ctx) */
00079 
00080 typedef int iconv_mod_event_t(struct iconv_module *, int, apr_pool_t *ctx);
00081 
00082 struct iconv_module_desc {
00083         int             imd_type;
00084         iconv_mod_event_t *imd_event;
00085         const struct iconv_module_depend *imd_depend;
00086         const void *    imd_data;
00087 };
00088 
00089 #define END_ICONV_MODULE_DEPEND  {0, NULL, NULL}
00090 
00091 #define ICONV_MODULE(type,data) struct iconv_module_desc iconv_module \
00092                                     {(type), (data)}
00093 
00094 struct iconv_module {
00095         int             im_flags;
00096         void *          im_handle;
00097         struct iconv_module_desc *im_desc;
00098         struct iconv_module *im_next;
00099         struct iconv_module *im_deplist;
00100         int             im_depcnt;
00101         const void *    im_methods;
00102         void *          im_data;
00103         const void *    im_depdata;     /* data if module loaded from dependency */
00104         const void *    im_args;
00105 
00106         /* This is module-private data. Nothing outside the module
00107            itself may touch it. */
00108         void *im_private;
00109 };
00110 
00111 #define ICONV_MOD_LOAD(mod,ctx) (mod)->im_desc->imd_event(mod, ICMODEV_LOAD,ctx)
00112 #define ICONV_MOD_UNLOAD(mod,ctx)       (mod)->im_desc->imd_event(mod, ICMODEV_UNLOAD,ctx)
00113 #define ICONV_MOD_DYN_LOAD(mod,ctx)     (mod)->im_desc->imd_event(mod, ICMODEV_DYN_LOAD,ctx)
00114 #define ICONV_MOD_DYN_UNLOAD(mod,ctx)   (mod)->im_desc->imd_event(mod, ICMODEV_DYN_UNLOAD,ctx)
00115 
00116 /*
00117  * iconv converter definitions.
00118  */
00119 typedef int iconv_open_t(const char *, const char *, void **, apr_pool_t *);
00120 typedef int iconv_close_t(void *, apr_pool_t *);
00121 typedef apr_status_t iconv_conv_t(void *, const unsigned char **, apr_size_t *,
00122         unsigned char **, apr_size_t *, apr_size_t *);
00123 
00124 struct iconv_converter_desc {
00125         iconv_open_t *  icd_open;
00126         iconv_close_t * icd_close;
00127         iconv_conv_t *  icd_conv;
00128 };
00129 
00130 struct iconv_converter {
00131         struct iconv_converter_desc *ic_desc;
00132         void *          ic_data;
00133 };
00134 
00135 /*
00136  * internal data types and functions used by charset conversion modules
00137  */
00138 
00139 #define UCS_CHAR_ZERO_WIDTH_NBSP 0xFEFF
00140 #define UCS_CHAR_INVALID         0xFFFE
00141 #define UCS_CHAR_NONE            0xFFFF
00142 
00143 typedef apr_uint16_t ucs2_t;    /* Unicode character [D5] */
00144 typedef apr_uint32_t ucs4_t;    /* Unicode scalar character [D28] */
00145 #define ucs_t    ucs4_t
00146 
00147 /*
00148  * one-level coded character set conversion tables
00149  */
00150 typedef struct {
00151         apr_uint16_t    data[128];
00152 } iconv_ccs_convtable_7bit;     /* 7-bit charset to Unicode */
00153 
00154 typedef struct {
00155         apr_uint16_t    data[256];
00156 } iconv_ccs_convtable_8bit;     /* 8-bit charset to Unicode */
00157 
00158 /*
00159  * two-level coded character set conversion tables
00160  */
00161 typedef struct {
00162         const iconv_ccs_convtable_7bit  *data[128];
00163 } iconv_ccs_convtable_14bit;    /* 14-bit charset to Unicode */
00164 
00165 typedef struct {
00166         const iconv_ccs_convtable_8bit  *data[256];
00167 } iconv_ccs_convtable_16bit;    /* 16-bit charset to Unicode;
00168                                  * Unicode to any charset */
00169 
00170 /*
00171  * abstract coded character set conversion table
00172  */
00173 typedef union {
00174         iconv_ccs_convtable_7bit        _7bit;
00175         iconv_ccs_convtable_8bit        _8bit;
00176         iconv_ccs_convtable_14bit       _14bit;
00177         iconv_ccs_convtable_16bit       _16bit;
00178 } iconv_ccs_convtable;
00179 
00180 /*
00181  * function for table-driven conversion
00182  */
00183 typedef ucs2_t iconv_ccs_converter_t (ucs2_t ch);
00184 
00185 /*
00186  * charset conversion module descriptor
00187  */
00188 struct iconv_ccs_desc {
00189         const char * const *            names;
00190         int                             nbits;
00191         const iconv_ccs_convtable *     from_ucs;
00192         const iconv_ccs_convtable *     to_ucs;
00193         iconv_ccs_converter_t *         convert_from_ucs;
00194         iconv_ccs_converter_t *         convert_to_ucs;
00195 };
00196 
00197 /*
00198  * inline functions for use in charset conversion modules
00199  */
00200 static APR_INLINE ucs2_t
00201 iconv_ccs_convert_7bit(const iconv_ccs_convtable *table, ucs2_t ch)
00202 {
00203         return ch & 0x80 ? UCS_CHAR_INVALID : table->_7bit.data[ch];
00204 }
00205 
00206 static APR_INLINE ucs2_t
00207 iconv_ccs_convert_8bit(const iconv_ccs_convtable *table, ucs2_t ch)
00208 {
00209         return table->_8bit.data[ch];
00210 }
00211 
00212 static APR_INLINE ucs2_t
00213 iconv_ccs_convert_14bit(const iconv_ccs_convtable *table, ucs2_t ch)
00214 {
00215         const iconv_ccs_convtable_7bit *sub_table;
00216 
00217         sub_table =  ch & 0x8080 ? NULL : table->_14bit.data[ch >> 8];
00218         return sub_table ? sub_table->data[ch & 0x7F] : UCS_CHAR_INVALID;
00219 }
00220 
00221 static APR_INLINE ucs2_t
00222 iconv_ccs_convert_16bit(const iconv_ccs_convtable *table, ucs2_t ch)
00223 {
00224         const iconv_ccs_convtable_8bit *sub_table;
00225 
00226         sub_table = table->_16bit.data[ch >> 8];
00227         return sub_table ? sub_table->data[ch & 0xFF] : UCS_CHAR_INVALID;
00228 }
00229 
00230 #define ICONV_CCS_CONVERT_FROM_UCS(ccsd, ch) \
00231         ((ccsd)->convert_from_ucs(ch))
00232 #define ICONV_CCS_CONVERT_TO_UCS(ccsd, ch) \
00233         ((ccsd)->convert_to_ucs(ch))
00234 
00235 /*
00236  * generic character encoding schemes and functions
00237  */
00238 struct iconv_ces;
00239 
00240 typedef int  iconv_ces_open_t(struct iconv_ces *, apr_pool_t *);
00241 typedef int  iconv_ces_close_t(struct iconv_ces *);
00242 typedef void iconv_ces_reset_t(struct iconv_ces *);
00243 typedef const char * const *iconv_ces_names_t(struct iconv_ces *);
00244 typedef int  iconv_ces_nbits_t(struct iconv_ces *);
00245 typedef int  iconv_ces_nbytes_t(struct iconv_ces *);
00246 
00247 typedef apr_ssize_t iconv_ces_convert_from_ucs_t
00248     (struct iconv_ces *data, ucs_t in,
00249     unsigned char **outbuf, apr_size_t *outbytesleft);
00250 
00251 typedef ucs_t iconv_ces_convert_to_ucs_t
00252     (struct iconv_ces *data,
00253     const unsigned char **inbuf, apr_size_t *inbytesleft);
00254 
00255 struct iconv_ces_desc {
00256         iconv_ces_open_t *      open;
00257         iconv_ces_close_t *     close;
00258         iconv_ces_reset_t *     reset;
00259         iconv_ces_names_t *     names;
00260         iconv_ces_nbits_t *     nbits;
00261         iconv_ces_nbytes_t *    nbytes;
00262         iconv_ces_convert_from_ucs_t * convert_from_ucs;
00263         iconv_ces_convert_to_ucs_t * convert_to_ucs;
00264         const void *data;
00265 };
00266 
00267 struct iconv_ces {
00268         struct iconv_ces_desc * desc;
00269         void *                  data;
00270         struct iconv_module *   mod;
00271 };
00272 
00273 API_DECLARE_NONSTD(int)  apr_iconv_ces_open(const char *ces_name, struct iconv_ces **cespp, apr_pool_t *ctx);
00274 API_DECLARE_NONSTD(int)  apr_iconv_ces_close(struct iconv_ces *ces, apr_pool_t *ctx);
00275 API_DECLARE_NONSTD(int)  apr_iconv_ces_open_func(struct iconv_ces *ces, apr_pool_t *ctx);
00276 API_DECLARE_NONSTD(int)  apr_iconv_ces_close_func(struct iconv_ces *ces);
00277 API_DECLARE_NONSTD(void) apr_iconv_ces_reset_func(struct iconv_ces *ces);
00278 API_DECLARE_NONSTD(void) apr_iconv_ces_no_func(struct iconv_ces *ces);
00279 API_DECLARE_NONSTD(int)  apr_iconv_ces_nbits7(struct iconv_ces *ces);
00280 API_DECLARE_NONSTD(int)  apr_iconv_ces_nbits8(struct iconv_ces *ces);
00281 API_DECLARE_NONSTD(int)  apr_iconv_ces_zero(struct iconv_ces *ces);
00282 
00283 /* Safe for _NONSTD APIs since the C caller pops the stack and the
00284  * apr_pool_t *cxt arg is simply ignored:
00285  */
00286 #define apr_iconv_ces_open_zero (iconv_ces_open_t*)apr_iconv_ces_zero
00287 
00288 #define iconv_char32bit(ch)     ((ch) & 0xFFFF0000)
00289 
00290 #define ICONV_CES_OPEN(ces,ctx) (ces)->desc->open(ces,ctx)
00291 #define ICONV_CES_CLOSE(ces)    (ces)->desc->close(ces)
00292 #define ICONV_CES_RESET(ces)    (ces)->desc->reset(ces)
00293 #define ICONV_CES_CONVERT_FROM_UCS(cesd, in, outbuf, outbytes) \
00294             ((cesd)->desc->convert_from_ucs((cesd), (in), (outbuf), (outbytes)))
00295 #define ICONV_CES_CONVERT_TO_UCS(cesd, inbuf, inbytes) \
00296             ((cesd)->desc->convert_to_ucs((cesd), (inbuf), (inbytes)))
00297 
00298 #define ICONV_CES_DRIVER_DECL(name) \
00299                 iconv_ces_open_t iconv_##name##_open; \
00300                 iconv_ces_close_t iconv_##name##_close; \
00301                 iconv_ces_reset_t iconv_##name##_reset; \
00302                 iconv_ces_nbits_t iconv_##name##_nbits; \
00303                 iconv_ces_convert_from_ucs_t iconv_##name##_convert_from_ucs; \
00304                 iconv_ces_convert_to_ucs_t iconv_##name##_convert_to_ucs
00305 
00306 /*
00307  ************************************************
00308  * EUC character encoding schemes and functions *
00309  ************************************************
00310  */
00311 
00312 typedef struct iconv_ces_euc_ccs {
00313         const char      *prefix;
00314         apr_size_t              prefixlen;
00315 } iconv_ces_euc_ccs_t;
00316 
00317 ICONV_CES_DRIVER_DECL(euc);
00318 
00319 /*
00320  * ISO-2022 character encoding schemes and functions
00321  */
00322 enum { ICONV_SHIFT_SI = 0, ICONV_SHIFT_SO, ICONV_SHIFT_SS2, ICONV_SHIFT_SS3 };
00323 
00324 typedef struct iconv_ces_iso2022_ccs {
00325         int             shift;
00326         const char *    designator;
00327         apr_size_t              designatorlen;
00328 } iconv_ces_iso2022_ccs_t;
00329 
00330 typedef struct {
00331         const int *     shift_tab;
00332         int             nccs;
00333         const struct iconv_ces_iso2022_ccs *ccs;
00334 } iconv_ces_iso2022_data;
00335 
00336 ICONV_CES_DRIVER_DECL(iso2022);
00337 
00338 
00339 API_DECLARE_NONSTD(int) apr_iconv_mod_load(const char *, int, const void *, struct iconv_module **, apr_pool_t *);
00340 API_DECLARE_NONSTD(int) apr_iconv_mod_unload(struct iconv_module *,apr_pool_t *ctx);
00341 API_DECLARE_NONSTD(int) apr_iconv_mod_noevent(struct iconv_module *mod, int event, apr_pool_t *ctx);
00342 API_DECLARE_NONSTD(int) apr_iconv_ccs_event(struct iconv_module *mod, int event, apr_pool_t *ctx);
00343 
00344 int  iconv_malloc(apr_size_t size, void **pp);
00345 
00346 extern struct iconv_converter_desc iconv_uc_desc;
00347 
00348 
00349 API_DECLARE_NONSTD(apr_status_t) apr_iconv_euc_open(struct iconv_ces *ces, apr_pool_t *ctx);
00350 API_DECLARE_NONSTD(apr_status_t) apr_iconv_euc_close(struct iconv_ces *ces);
00351 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);
00352 API_DECLARE_NONSTD(ucs_t)  apr_iconv_euc_convert_to_ucs(struct iconv_ces *ces, const unsigned char **inbuf, apr_size_t *inbytesleft);
00353 
00354 API_DECLARE_NONSTD(apr_status_t) apr_iconv_iso2022_open(struct iconv_ces *ces, apr_pool_t *ctx);
00355 API_DECLARE_NONSTD(int) apr_iconv_iso2022_close(struct iconv_ces *ces);
00356 API_DECLARE_NONSTD(void) apr_iconv_iso2022_reset(struct iconv_ces *ces);
00357 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);
00358 API_DECLARE_NONSTD(ucs_t) apr_iconv_iso2022_convert_to_ucs(struct iconv_ces *ces, const unsigned char **inbuf, apr_size_t *inbytesleft);
00359 
00360 #endif /* ICONV_INTERNAL */
00361 
00362 #endif /* _ICONV_H_ */

Generated on Wed Dec 27 11:20:19 2006 for Apache Portable Runtime Iconv Library by  doxygen 1.5.1