Home | History | Annotate | Line # | Download | only in citrus
citrus_stdenc.c revision 1.3.44.1
      1  1.3.44.1      yamt /*	$NetBSD: citrus_stdenc.c,v 1.3.44.1 2012/04/17 00:05:16 yamt Exp $	*/
      2       1.1  tshiozak 
      3       1.1  tshiozak /*-
      4       1.1  tshiozak  * Copyright (c)2003 Citrus Project,
      5       1.1  tshiozak  * All rights reserved.
      6       1.1  tshiozak  *
      7       1.1  tshiozak  * Redistribution and use in source and binary forms, with or without
      8       1.1  tshiozak  * modification, are permitted provided that the following conditions
      9       1.1  tshiozak  * are met:
     10       1.1  tshiozak  * 1. Redistributions of source code must retain the above copyright
     11       1.1  tshiozak  *    notice, this list of conditions and the following disclaimer.
     12       1.1  tshiozak  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  tshiozak  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  tshiozak  *    documentation and/or other materials provided with the distribution.
     15       1.1  tshiozak  *
     16       1.1  tshiozak  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17       1.1  tshiozak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18       1.1  tshiozak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.1  tshiozak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20       1.1  tshiozak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21       1.1  tshiozak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22       1.1  tshiozak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.1  tshiozak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24       1.1  tshiozak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25       1.1  tshiozak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26       1.1  tshiozak  * SUCH DAMAGE.
     27       1.1  tshiozak  */
     28       1.1  tshiozak 
     29       1.1  tshiozak #include <sys/cdefs.h>
     30       1.1  tshiozak #if defined(LIBC_SCCS) && !defined(lint)
     31  1.3.44.1      yamt __RCSID("$NetBSD: citrus_stdenc.c,v 1.3.44.1 2012/04/17 00:05:16 yamt Exp $");
     32       1.1  tshiozak #endif /* LIBC_SCCS and not lint */
     33       1.1  tshiozak 
     34       1.1  tshiozak #include "namespace.h"
     35       1.1  tshiozak #include <assert.h>
     36       1.1  tshiozak #include <errno.h>
     37       1.1  tshiozak #include <stdlib.h>
     38       1.1  tshiozak #include <string.h>
     39       1.1  tshiozak 
     40       1.1  tshiozak #include "citrus_namespace.h"
     41       1.1  tshiozak #include "citrus_types.h"
     42       1.1  tshiozak #include "citrus_module.h"
     43       1.1  tshiozak #include "citrus_stdenc.h"
     44       1.1  tshiozak #include "citrus_none.h"
     45       1.1  tshiozak 
     46       1.1  tshiozak struct _citrus_stdenc _citrus_stdenc_default = {
     47       1.1  tshiozak 	&_citrus_NONE_stdenc_ops,	/* ce_ops */
     48       1.1  tshiozak 	NULL,				/* ce_closure */
     49       1.1  tshiozak 	NULL,				/* ce_module */
     50       1.1  tshiozak 	&_citrus_NONE_stdenc_traits,	/* ce_traits */
     51       1.1  tshiozak };
     52       1.1  tshiozak 
     53       1.1  tshiozak #ifdef _I18N_DYNAMIC
     54       1.1  tshiozak 
     55       1.3  tshiozak static int
     56       1.3  tshiozak /*ARGSUSED*/
     57       1.3  tshiozak get_state_desc_default(struct _citrus_stdenc * __restrict ce,
     58       1.3  tshiozak 		       void * __restrict ps,
     59       1.3  tshiozak 		       int id,
     60       1.3  tshiozak 		       struct _citrus_stdenc_state_desc * __restrict d)
     61       1.3  tshiozak {
     62       1.3  tshiozak 	return EOPNOTSUPP;
     63       1.3  tshiozak }
     64       1.3  tshiozak 
     65       1.1  tshiozak int
     66       1.1  tshiozak _citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict rce,
     67       1.1  tshiozak 		    char const * __restrict encname,
     68       1.1  tshiozak 		    const void * __restrict variable, size_t lenvar)
     69       1.1  tshiozak {
     70       1.1  tshiozak 	int ret;
     71       1.1  tshiozak 	_citrus_module_t handle;
     72       1.1  tshiozak 	struct _citrus_stdenc *ce;
     73       1.1  tshiozak 	_citrus_stdenc_getops_t getops;
     74       1.1  tshiozak 
     75       1.1  tshiozak 	_DIAGASSERT(encname != NULL);
     76       1.1  tshiozak 	_DIAGASSERT(!lenvar || variable!=NULL);
     77       1.2  tshiozak 	_DIAGASSERT(rce != NULL);
     78       1.1  tshiozak 
     79       1.1  tshiozak 	if (!strcmp(encname, _CITRUS_DEFAULT_STDENC_NAME)) {
     80       1.1  tshiozak 		*rce = &_citrus_stdenc_default;
     81       1.1  tshiozak 		return (0);
     82       1.1  tshiozak 	}
     83       1.1  tshiozak 	ce = malloc(sizeof(*ce));
     84       1.1  tshiozak 	if (ce==NULL) {
     85       1.1  tshiozak 		ret = errno;
     86       1.1  tshiozak 		goto bad;
     87       1.1  tshiozak 	}
     88       1.1  tshiozak 	ce->ce_ops = NULL;
     89       1.1  tshiozak 	ce->ce_closure = NULL;
     90       1.1  tshiozak 	ce->ce_module = NULL;
     91       1.1  tshiozak 	ce->ce_traits = NULL;
     92       1.1  tshiozak 
     93       1.1  tshiozak 	ret = _citrus_load_module(&handle, encname);
     94       1.1  tshiozak 	if (ret)
     95       1.1  tshiozak 		goto bad;
     96       1.1  tshiozak 
     97       1.1  tshiozak 	ce->ce_module = handle;
     98       1.1  tshiozak 
     99       1.1  tshiozak 	getops =
    100       1.1  tshiozak 	    (_citrus_stdenc_getops_t)_citrus_find_getops(ce->ce_module,
    101       1.1  tshiozak 							 encname, "stdenc");
    102       1.1  tshiozak 	if (getops == NULL) {
    103       1.1  tshiozak 		ret = EINVAL;
    104       1.1  tshiozak 		goto bad;
    105       1.1  tshiozak 	}
    106       1.1  tshiozak 
    107       1.1  tshiozak 	ce->ce_ops = (struct _citrus_stdenc_ops *)malloc(sizeof(*ce->ce_ops));
    108       1.1  tshiozak 	if (ce->ce_ops == NULL) {
    109       1.1  tshiozak 		ret = errno;
    110       1.1  tshiozak 		goto bad;
    111       1.1  tshiozak 	}
    112       1.1  tshiozak 
    113       1.1  tshiozak 	ret = (*getops)(ce->ce_ops, sizeof(*ce->ce_ops),
    114       1.1  tshiozak 			_CITRUS_STDENC_ABI_VERSION);
    115       1.1  tshiozak 	if (ret)
    116       1.1  tshiozak 		goto bad;
    117       1.1  tshiozak 
    118       1.1  tshiozak 	/* If return ABI version is not expected, should fixup it */
    119       1.3  tshiozak 	if (ce->ce_ops->eo_abi_version < 0x00000002) {
    120       1.3  tshiozak 		ce->ce_ops->eo_get_state_desc = &get_state_desc_default;
    121       1.3  tshiozak 	}
    122       1.1  tshiozak 
    123       1.1  tshiozak 	/* validation check */
    124       1.1  tshiozak 	if (ce->ce_ops->eo_init == NULL ||
    125       1.1  tshiozak 	    ce->ce_ops->eo_uninit == NULL ||
    126       1.1  tshiozak 	    ce->ce_ops->eo_init_state == NULL ||
    127       1.1  tshiozak 	    ce->ce_ops->eo_mbtocs == NULL ||
    128       1.1  tshiozak 	    ce->ce_ops->eo_cstomb == NULL ||
    129       1.1  tshiozak 	    ce->ce_ops->eo_mbtowc == NULL ||
    130       1.3  tshiozak 	    ce->ce_ops->eo_wctomb == NULL ||
    131  1.3.44.1      yamt 	    ce->ce_ops->eo_get_state_desc == NULL) {
    132  1.3.44.1      yamt 		ret = EINVAL;
    133       1.1  tshiozak 		goto bad;
    134  1.3.44.1      yamt 	}
    135       1.1  tshiozak 
    136       1.1  tshiozak 	/* allocate traits */
    137       1.1  tshiozak 	ce->ce_traits = malloc(sizeof(*ce->ce_traits));
    138       1.1  tshiozak 	if (ce->ce_traits == NULL) {
    139       1.1  tshiozak 		ret = errno;
    140       1.1  tshiozak 		goto bad;
    141       1.1  tshiozak 	}
    142       1.1  tshiozak 	/* init and get closure */
    143       1.1  tshiozak 	ret = (*ce->ce_ops->eo_init)(ce, variable, lenvar, ce->ce_traits);
    144       1.1  tshiozak 	if (ret)
    145       1.1  tshiozak 		goto bad;
    146       1.1  tshiozak 
    147       1.1  tshiozak 	*rce = ce;
    148       1.1  tshiozak 
    149       1.1  tshiozak 	return (0);
    150       1.1  tshiozak 
    151       1.1  tshiozak bad:
    152       1.1  tshiozak 	_citrus_stdenc_close(ce);
    153       1.1  tshiozak 	return (ret);
    154       1.1  tshiozak }
    155       1.1  tshiozak 
    156       1.1  tshiozak void
    157       1.1  tshiozak _citrus_stdenc_close(struct _citrus_stdenc *ce)
    158       1.1  tshiozak {
    159       1.1  tshiozak 
    160       1.2  tshiozak 	_DIAGASSERT(ce != NULL);
    161       1.1  tshiozak 
    162       1.1  tshiozak 	if (ce == &_citrus_stdenc_default)
    163       1.1  tshiozak 		return;
    164       1.1  tshiozak 
    165       1.1  tshiozak 	if (ce->ce_module) {
    166       1.1  tshiozak 		if (ce->ce_ops) {
    167       1.1  tshiozak 			if (ce->ce_closure && ce->ce_ops->eo_uninit)
    168       1.1  tshiozak 				(*ce->ce_ops->eo_uninit)(ce);
    169       1.1  tshiozak 			free(ce->ce_ops);
    170       1.1  tshiozak 		}
    171       1.1  tshiozak 		free(ce->ce_traits);
    172       1.1  tshiozak 		_citrus_unload_module(ce->ce_module);
    173       1.1  tshiozak 	}
    174       1.1  tshiozak 	free(ce);
    175       1.1  tshiozak }
    176       1.1  tshiozak 
    177       1.1  tshiozak #else
    178       1.1  tshiozak /* !_I18N_DYNAMIC */
    179       1.1  tshiozak 
    180       1.1  tshiozak int
    181       1.1  tshiozak /*ARGSUSED*/
    182       1.1  tshiozak _citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict rce,
    183       1.1  tshiozak 		    char const * __restrict encname,
    184       1.1  tshiozak 		    const void * __restrict variable, size_t lenvar)
    185       1.1  tshiozak {
    186       1.1  tshiozak 	if (!strcmp(encname, _CITRUS_DEFAULT_STDENC_NAME)) {
    187       1.1  tshiozak 		*rce = &_citrus_stdenc_default;
    188       1.1  tshiozak 		return (0);
    189       1.1  tshiozak 	}
    190       1.1  tshiozak 	return (EINVAL);
    191       1.1  tshiozak }
    192       1.1  tshiozak 
    193       1.1  tshiozak void
    194       1.1  tshiozak /*ARGSUSED*/
    195       1.1  tshiozak _citrus_stdenc_close(struct _citrus_stdenc *ce)
    196       1.1  tshiozak {
    197       1.1  tshiozak }
    198       1.1  tshiozak 
    199       1.1  tshiozak #endif
    200