Home | History | Annotate | Line # | Download | only in iconv
iconv.c revision 1.1
      1 /*	$NetBSD: iconv.c,v 1.1 2003/06/27 05:21:53 tshiozak Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2003 Citrus Project,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #if defined(LIBC_SCCS) && !defined(lint)
     31 __RCSID("$NetBSD: iconv.c,v 1.1 2003/06/27 05:21:53 tshiozak Exp $");
     32 #endif /* LIBC_SCCS and not lint */
     33 
     34 #include "namespace.h"
     35 #include <assert.h>
     36 #include <errno.h>
     37 #include <paths.h>
     38 
     39 #include <iconv.h>
     40 
     41 #ifdef CITRUS_ICONV
     42 #include <sys/types.h>
     43 #include <citrus/citrus_types.h>
     44 #include <citrus/citrus_module.h>
     45 #include <citrus/citrus_esdb.h>
     46 #include <citrus/citrus_iconv.h>
     47 
     48 #define ISBADF(_h_)	(!(_h_) || (_h_) == (iconv_t)-1)
     49 
     50 #ifdef __weak_alias
     51 __weak_alias(iconv, _iconv)
     52 __weak_alias(iconv_open, _iconv_open)
     53 __weak_alias(iconv_close, _iconv_close)
     54 #endif
     55 
     56 iconv_t
     57 _iconv_open(const char *out, const char *in)
     58 {
     59 	int ret;
     60 	struct _citrus_iconv *handle;
     61 
     62 	ret = _citrus_iconv_open(&handle, _PATH_ICONV, in, out);
     63 	if (ret) {
     64 		errno = ret;
     65 		return ((iconv_t)-1);
     66 	}
     67 
     68 	return ((iconv_t)handle);
     69 }
     70 
     71 int
     72 _iconv_close(iconv_t handle)
     73 {
     74 	if (ISBADF(handle)) {
     75 		errno = EBADF;
     76 		return (-1);
     77 	}
     78 
     79 	_citrus_iconv_close((struct _citrus_iconv *)handle);
     80 
     81 	return (0);
     82 }
     83 
     84 size_t
     85 _iconv(iconv_t handle, const char **in, size_t *szin, char **out, size_t *szout)
     86 {
     87 	int err;
     88 	size_t ret;
     89 
     90 	if (ISBADF(handle)) {
     91 		errno = EBADF;
     92 		return ((size_t)-1);
     93 	}
     94 
     95 	err = _citrus_iconv_convert(
     96 		(struct _citrus_iconv *)handle, in, szin, out, szout, 0, &ret);
     97 	if (err) {
     98 		errno = err;
     99 		ret = (size_t)-1;
    100 	}
    101 
    102 	return (ret);
    103 }
    104 
    105 size_t
    106 __iconv(iconv_t handle, const char **in, size_t *szin, char **out,
    107 	size_t *szout, u_int32_t flags, size_t *invalids)
    108 {
    109 	int err;
    110 	size_t ret;
    111 
    112 	if (ISBADF(handle)) {
    113 		errno = EBADF;
    114 		return ((size_t)-1);
    115 	}
    116 
    117 	err = _citrus_iconv_convert(
    118 		(struct _citrus_iconv *)handle, in, szin, out, szout,
    119 		flags, &ret);
    120 	if (invalids)
    121 		*invalids = ret;
    122 	if (err) {
    123 		errno = err;
    124 		ret = (size_t)-1;
    125 	}
    126 
    127 	return (ret);
    128 }
    129 
    130 int
    131 __iconv_get_list(char ***rlist, size_t *rsz)
    132 {
    133 	int ret;
    134 
    135 	ret = _citrus_esdb_get_list(rlist, rsz);
    136 	if (ret) {
    137 		errno = ret;
    138 		return -1;
    139 	}
    140 
    141 	return 0;
    142 }
    143 
    144 void
    145 __iconv_free_list(char **list, size_t sz)
    146 {
    147 	_citrus_esdb_free_list(list, sz);
    148 }
    149 
    150 #else
    151 iconv_t
    152 /*ARGSUSED*/
    153 _iconv_open(const char *in, const char *out)
    154 {
    155 	errno = EINVAL;
    156 	return ((iconv_t)-1);
    157 }
    158 int
    159 /*ARGSUSED*/
    160 _iconv_close(iconv_t handle)
    161 {
    162 	errno = EBADF;
    163 	return (-1);
    164 }
    165 size_t
    166 /*ARGSUSED*/
    167 _iconv(iconv_t handle, const char **in, size_t *szin, char **out, size_t *szout)
    168 {
    169 	errno = EBADF;
    170 	return ((size_t)-1);
    171 }
    172 int
    173 /*ARGSUSED*/
    174 __iconv_get_list(char ***rlist, size_t *rsz)
    175 {
    176 	errno = EINVAL;
    177 	return -1;
    178 }
    179 void
    180 /*ARGSUSED*/
    181 __iconv_free_list(char **list, size_t sz)
    182 {
    183 }
    184 #endif
    185