From 2180e8af174c41b1875a1b361506ccc885986534 Mon Sep 17 00:00:00 2001 From: Joerg Sonnenberger Date: Fri, 11 Mar 2005 23:33:53 +0000 Subject: [PATCH] Add citrus backend code and iconv front end. This is intentionally not hooked into the build yet. Obtained-from: NetBSD / Citrus Project --- include/iconv.h | 54 + lib/libc/citrus/Makefile.inc | 12 + lib/libc/citrus/citrus_bcs.c | 133 ++ lib/libc/citrus/citrus_bcs.h | 81 + lib/libc/citrus/citrus_csmapper.c | 388 +++++ lib/libc/citrus/citrus_csmapper.h | 49 + lib/libc/citrus/citrus_ctype.c | 207 +++ lib/libc/citrus/citrus_ctype.h | 171 +++ lib/libc/citrus/citrus_ctype_fallback.c | 104 ++ lib/libc/citrus/citrus_ctype_fallback.h | 39 + lib/libc/citrus/citrus_ctype_local.h | 200 +++ lib/libc/citrus/citrus_ctype_template.h | 769 ++++++++++ lib/libc/citrus/citrus_db.c | 338 +++++ lib/libc/citrus/citrus_db.h | 76 + lib/libc/citrus/citrus_db_factory.c | 345 +++++ lib/libc/citrus/citrus_db_factory.h | 60 + lib/libc/citrus/citrus_db_file.h | 86 ++ lib/libc/citrus/citrus_db_hash.c | 61 + lib/libc/citrus/citrus_db_hash.h | 38 + lib/libc/citrus/citrus_esdb.c | 351 +++++ lib/libc/citrus/citrus_esdb.h | 57 + lib/libc/citrus/citrus_esdb_file.h | 46 + lib/libc/citrus/citrus_hash.c | 51 + lib/libc/citrus/citrus_hash.h | 60 + lib/libc/citrus/citrus_iconv.c | 391 +++++ lib/libc/citrus/citrus_iconv.h | 69 + lib/libc/citrus/citrus_iconv_local.h | 108 ++ lib/libc/citrus/citrus_lookup.c | 353 +++++ lib/libc/citrus/citrus_lookup.h | 62 + lib/libc/citrus/citrus_lookup_factory.c | 120 ++ lib/libc/citrus/citrus_lookup_factory.h | 37 + lib/libc/citrus/citrus_lookup_file.h | 35 + lib/libc/citrus/citrus_mapper.c | 412 ++++++ lib/libc/citrus/citrus_mapper.h | 143 ++ lib/libc/citrus/citrus_mapper_local.h | 106 ++ lib/libc/citrus/citrus_memstream.c | 149 ++ lib/libc/citrus/citrus_memstream.h | 176 +++ lib/libc/citrus/citrus_mmap.c | 96 ++ lib/libc/citrus/citrus_mmap.h | 40 + lib/libc/citrus/citrus_module.c | 358 +++++ lib/libc/citrus/citrus_module.h | 44 + lib/libc/citrus/citrus_namespace.h | 224 +++ lib/libc/citrus/citrus_none.c | 476 ++++++ lib/libc/citrus/citrus_none.h | 37 + lib/libc/citrus/citrus_pivot_factory.c | 222 +++ lib/libc/citrus/citrus_pivot_factory.h | 37 + lib/libc/citrus/citrus_pivot_file.h | 36 + lib/libc/citrus/citrus_region.h | 94 ++ lib/libc/citrus/citrus_stdenc.c | 179 +++ lib/libc/citrus/citrus_stdenc.h | 119 ++ lib/libc/citrus/citrus_stdenc_local.h | 146 ++ lib/libc/citrus/citrus_stdenc_template.h | 187 +++ lib/libc/citrus/citrus_types.h | 38 + lib/libc/citrus/modules/citrus_big5.c | 395 +++++ lib/libc/citrus/modules/citrus_big5.h | 38 + lib/libc/citrus/modules/citrus_euc.c | 414 ++++++ lib/libc/citrus/modules/citrus_euc.h | 38 + lib/libc/citrus/modules/citrus_euctw.c | 413 ++++++ lib/libc/citrus/modules/citrus_euctw.h | 38 + lib/libc/citrus/modules/citrus_gbk2k.c | 456 ++++++ lib/libc/citrus/modules/citrus_gbk2k.h | 38 + lib/libc/citrus/modules/citrus_iconv_none.c | 124 ++ lib/libc/citrus/modules/citrus_iconv_none.h | 37 + lib/libc/citrus/modules/citrus_iconv_std.c | 554 +++++++ lib/libc/citrus/modules/citrus_iconv_std.h | 37 + .../citrus/modules/citrus_iconv_std_local.h | 82 ++ lib/libc/citrus/modules/citrus_iso2022.c | 1299 +++++++++++++++++ lib/libc/citrus/modules/citrus_iso2022.h | 38 + lib/libc/citrus/modules/citrus_mapper_646.c | 256 ++++ lib/libc/citrus/modules/citrus_mapper_646.h | 38 + lib/libc/citrus/modules/citrus_mapper_none.c | 108 ++ lib/libc/citrus/modules/citrus_mapper_none.h | 37 + .../citrus/modules/citrus_mapper_serial.c | 260 ++++ .../citrus/modules/citrus_mapper_serial.h | 38 + lib/libc/citrus/modules/citrus_mapper_std.c | 342 +++++ lib/libc/citrus/modules/citrus_mapper_std.h | 39 + .../citrus/modules/citrus_mapper_std_file.h | 62 + .../citrus/modules/citrus_mapper_std_local.h | 67 + lib/libc/citrus/modules/citrus_mapper_zone.c | 398 +++++ lib/libc/citrus/modules/citrus_mapper_zone.h | 37 + lib/libc/citrus/modules/citrus_mskanji.c | 455 ++++++ lib/libc/citrus/modules/citrus_mskanji.h | 38 + lib/libc/citrus/modules/citrus_utf1632.c | 434 ++++++ lib/libc/citrus/modules/citrus_utf1632.h | 37 + lib/libc/citrus/modules/citrus_utf7.c | 574 ++++++++ lib/libc/citrus/modules/citrus_utf7.h | 38 + lib/libc/citrus/modules/citrus_utf8.c | 395 +++++ lib/libc/citrus/modules/citrus_utf8.h | 38 + lib/libc/iconv/Makefile.inc | 7 + lib/libc/iconv/iconv.3 | 245 ++++ lib/libc/iconv/iconv.c | 143 ++ 91 files changed, 16357 insertions(+) create mode 100644 include/iconv.h create mode 100644 lib/libc/citrus/Makefile.inc create mode 100644 lib/libc/citrus/citrus_bcs.c create mode 100644 lib/libc/citrus/citrus_bcs.h create mode 100644 lib/libc/citrus/citrus_csmapper.c create mode 100644 lib/libc/citrus/citrus_csmapper.h create mode 100644 lib/libc/citrus/citrus_ctype.c create mode 100644 lib/libc/citrus/citrus_ctype.h create mode 100644 lib/libc/citrus/citrus_ctype_fallback.c create mode 100644 lib/libc/citrus/citrus_ctype_fallback.h create mode 100644 lib/libc/citrus/citrus_ctype_local.h create mode 100644 lib/libc/citrus/citrus_ctype_template.h create mode 100644 lib/libc/citrus/citrus_db.c create mode 100644 lib/libc/citrus/citrus_db.h create mode 100644 lib/libc/citrus/citrus_db_factory.c create mode 100644 lib/libc/citrus/citrus_db_factory.h create mode 100644 lib/libc/citrus/citrus_db_file.h create mode 100644 lib/libc/citrus/citrus_db_hash.c create mode 100644 lib/libc/citrus/citrus_db_hash.h create mode 100644 lib/libc/citrus/citrus_esdb.c create mode 100644 lib/libc/citrus/citrus_esdb.h create mode 100644 lib/libc/citrus/citrus_esdb_file.h create mode 100644 lib/libc/citrus/citrus_hash.c create mode 100644 lib/libc/citrus/citrus_hash.h create mode 100644 lib/libc/citrus/citrus_iconv.c create mode 100644 lib/libc/citrus/citrus_iconv.h create mode 100644 lib/libc/citrus/citrus_iconv_local.h create mode 100644 lib/libc/citrus/citrus_lookup.c create mode 100644 lib/libc/citrus/citrus_lookup.h create mode 100644 lib/libc/citrus/citrus_lookup_factory.c create mode 100644 lib/libc/citrus/citrus_lookup_factory.h create mode 100644 lib/libc/citrus/citrus_lookup_file.h create mode 100644 lib/libc/citrus/citrus_mapper.c create mode 100644 lib/libc/citrus/citrus_mapper.h create mode 100644 lib/libc/citrus/citrus_mapper_local.h create mode 100644 lib/libc/citrus/citrus_memstream.c create mode 100644 lib/libc/citrus/citrus_memstream.h create mode 100644 lib/libc/citrus/citrus_mmap.c create mode 100644 lib/libc/citrus/citrus_mmap.h create mode 100644 lib/libc/citrus/citrus_module.c create mode 100644 lib/libc/citrus/citrus_module.h create mode 100644 lib/libc/citrus/citrus_namespace.h create mode 100644 lib/libc/citrus/citrus_none.c create mode 100644 lib/libc/citrus/citrus_none.h create mode 100644 lib/libc/citrus/citrus_pivot_factory.c create mode 100644 lib/libc/citrus/citrus_pivot_factory.h create mode 100644 lib/libc/citrus/citrus_pivot_file.h create mode 100644 lib/libc/citrus/citrus_region.h create mode 100644 lib/libc/citrus/citrus_stdenc.c create mode 100644 lib/libc/citrus/citrus_stdenc.h create mode 100644 lib/libc/citrus/citrus_stdenc_local.h create mode 100644 lib/libc/citrus/citrus_stdenc_template.h create mode 100644 lib/libc/citrus/citrus_types.h create mode 100644 lib/libc/citrus/modules/citrus_big5.c create mode 100644 lib/libc/citrus/modules/citrus_big5.h create mode 100644 lib/libc/citrus/modules/citrus_euc.c create mode 100644 lib/libc/citrus/modules/citrus_euc.h create mode 100644 lib/libc/citrus/modules/citrus_euctw.c create mode 100644 lib/libc/citrus/modules/citrus_euctw.h create mode 100644 lib/libc/citrus/modules/citrus_gbk2k.c create mode 100644 lib/libc/citrus/modules/citrus_gbk2k.h create mode 100644 lib/libc/citrus/modules/citrus_iconv_none.c create mode 100644 lib/libc/citrus/modules/citrus_iconv_none.h create mode 100644 lib/libc/citrus/modules/citrus_iconv_std.c create mode 100644 lib/libc/citrus/modules/citrus_iconv_std.h create mode 100644 lib/libc/citrus/modules/citrus_iconv_std_local.h create mode 100644 lib/libc/citrus/modules/citrus_iso2022.c create mode 100644 lib/libc/citrus/modules/citrus_iso2022.h create mode 100644 lib/libc/citrus/modules/citrus_mapper_646.c create mode 100644 lib/libc/citrus/modules/citrus_mapper_646.h create mode 100644 lib/libc/citrus/modules/citrus_mapper_none.c create mode 100644 lib/libc/citrus/modules/citrus_mapper_none.h create mode 100644 lib/libc/citrus/modules/citrus_mapper_serial.c create mode 100644 lib/libc/citrus/modules/citrus_mapper_serial.h create mode 100644 lib/libc/citrus/modules/citrus_mapper_std.c create mode 100644 lib/libc/citrus/modules/citrus_mapper_std.h create mode 100644 lib/libc/citrus/modules/citrus_mapper_std_file.h create mode 100644 lib/libc/citrus/modules/citrus_mapper_std_local.h create mode 100644 lib/libc/citrus/modules/citrus_mapper_zone.c create mode 100644 lib/libc/citrus/modules/citrus_mapper_zone.h create mode 100644 lib/libc/citrus/modules/citrus_mskanji.c create mode 100644 lib/libc/citrus/modules/citrus_mskanji.h create mode 100644 lib/libc/citrus/modules/citrus_utf1632.c create mode 100644 lib/libc/citrus/modules/citrus_utf1632.h create mode 100644 lib/libc/citrus/modules/citrus_utf7.c create mode 100644 lib/libc/citrus/modules/citrus_utf7.h create mode 100644 lib/libc/citrus/modules/citrus_utf8.c create mode 100644 lib/libc/citrus/modules/citrus_utf8.h create mode 100644 lib/libc/iconv/Makefile.inc create mode 100644 lib/libc/iconv/iconv.3 create mode 100644 lib/libc/iconv/iconv.c diff --git a/include/iconv.h b/include/iconv.h new file mode 100644 index 0000000000..29dee16326 --- /dev/null +++ b/include/iconv.h @@ -0,0 +1,54 @@ +/* $NetBSD: iconv.h,v 1.3 2003/08/22 14:05:03 kleink Exp $ */ +/* $DragonFly: src/include/iconv.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#ifndef _ICONV_H_ +#define _ICONV_H_ + +#include + +struct __tag_iconv_t; +typedef struct __tag_iconv_t *iconv_t; + +__BEGIN_DECLS +iconv_t iconv_open(const char *, const char *); +size_t iconv(iconv_t, char ** __restrict, size_t * __restrict, + char ** __restrict, size_t * __restrict); +int iconv_close(iconv_t); +/* + * non-portable interfaces for iconv + */ +int __iconv_get_list(char ***, size_t *); +void __iconv_free_list(char **, size_t); +size_t __iconv(iconv_t, const char **, size_t *, char **, size_t *, + __uint32_t, size_t *); +#define __ICONV_F_HIDE_INVALID 0x0001 +__END_DECLS + +#endif /* !_ICONV_H_ */ diff --git a/lib/libc/citrus/Makefile.inc b/lib/libc/citrus/Makefile.inc new file mode 100644 index 0000000000..9a02571419 --- /dev/null +++ b/lib/libc/citrus/Makefile.inc @@ -0,0 +1,12 @@ +# $DragonFly: src/lib/libc/citrus/Makefile.inc,v 1.1 2005/03/11 23:33:53 joerg Exp $ + +.PATH: ${.CURDIR}/../libc/citrus + +.include "../../i18n_module/Makefile.shlib" + +CFLAGS+= -DI18NMODULE_MAJOR=${MODULE_SHLIB_MAJOR} -D_I18N_DYNAMIC + +SRCS+= citrus_bcs.c citrus_csmapper.c citrus_ctype.c citrus_ctype_fallback.c +SRCS+= citrus_db.c citrus_db_hash.c citrus_esdb.c citrus_hash.c +SRCS+= citrus_iconv.c citrus_lookup.c citrus_mapper.c citrus_memstream.c +SRCS+= citrus_mmap.c citrus_module.c citrus_none.c citrus_stdenc.c diff --git a/lib/libc/citrus/citrus_bcs.c b/lib/libc/citrus/citrus_bcs.c new file mode 100644 index 0000000000..776203fda6 --- /dev/null +++ b/lib/libc/citrus/citrus_bcs.c @@ -0,0 +1,133 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_bcs.c,v 1.4 2004/01/02 21:49:35 itojun Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_bcs.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" + +int +_citrus_bcs_strcasecmp(const char * __restrict str1, + const char * __restrict str2) +{ + int c1 = 1, c2 = 1; + + while (c1 && c2 && c1 == c2) { + c1 = _bcs_toupper(*str1++); + c2 = _bcs_toupper(*str2++); + } + + return ((c1 == c2) ? 0 : ((c1 > c2) ? 1 : -1)); +} + +int +_citrus_bcs_strncasecmp(const char * __restrict str1, + const char * __restrict str2, size_t sz) +{ + int c1 = 1, c2 = 1; + + while (c1 && c2 && c1 == c2 && sz != 0) { + c1 = _bcs_toupper(*str1++); + c2 = _bcs_toupper(*str2++); + sz--; + } + + return ((c1 == c2) ? 0 : ((c1 > c2) ? 1 : -1)); +} + +const char * +_citrus_bcs_skip_ws(const char *p) +{ + + while (*p && _bcs_isspace(*p)) + p++; + + return (p); +} + +const char * +_citrus_bcs_skip_nonws(const char *p) +{ + + while (*p && !_bcs_isspace(*p)) + p++; + + return (p); +} + +const char * +_citrus_bcs_skip_ws_len(const char * __restrict p, size_t * __restrict len) +{ + + while (*p && *len > 0 && _bcs_isspace(*p)) { + p++; + (*len)--; + } + + return (p); +} + +const char * +_citrus_bcs_skip_nonws_len(const char * __restrict p, size_t * __restrict len) +{ + + while (*p && *len > 0 && !_bcs_isspace(*p)) { + p++; + (*len)--; + } + + return (p); +} + +void +_citrus_bcs_trunc_rws_len(const char * __restrict p, size_t * __restrict len) +{ + + while (*len > 0 && _bcs_isspace(p[*len - 1])) + (*len)--; +} + +void +_citrus_bcs_convert_to_lower(char *s) +{ + while (*s) { + *s = _bcs_tolower(*s); + s++; + } +} + +void _citrus_bcs_convert_to_upper(char *s) +{ + while (*s) { + *s = _bcs_toupper(*s); + s++; + } +} diff --git a/lib/libc/citrus/citrus_bcs.h b/lib/libc/citrus/citrus_bcs.h new file mode 100644 index 0000000000..972d53c025 --- /dev/null +++ b/lib/libc/citrus/citrus_bcs.h @@ -0,0 +1,81 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_bcs.h,v 1.2 2004/01/02 21:49:35 itojun Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_bcs.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_BCS_H_ +#define _CITRUS_BCS_H_ + +/* + * predicate/conversion for basic character set. + */ + +#define _CITRUS_BCS_PRED(_name_, _cond_) \ +static __inline int _citrus_bcs_##_name_(uint8_t c) { return (_cond_); } + +_CITRUS_BCS_PRED(isblank, c == ' ' || c == '\t') +_CITRUS_BCS_PRED(iseol, c == '\n' || c == '\r') +_CITRUS_BCS_PRED(isspace, + _citrus_bcs_isblank(c) || _citrus_bcs_iseol(c) || + c == '\f' || c == '\v') +_CITRUS_BCS_PRED(isdigit, c >= '0' && c <= '9') +_CITRUS_BCS_PRED(isupper, c >= 'A' && c <= 'Z') +_CITRUS_BCS_PRED(islower, c >= 'a' && c <= 'z') +_CITRUS_BCS_PRED(isalpha, _citrus_bcs_isupper(c) || _citrus_bcs_islower(c)) +_CITRUS_BCS_PRED(isalnum, _citrus_bcs_isdigit(c) || _citrus_bcs_isalpha(c)) +_CITRUS_BCS_PRED(isxdigit, + _citrus_bcs_isdigit(c) || + (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) + +static __inline uint8_t +_citrus_bcs_toupper(uint8_t c) +{ + return (_citrus_bcs_islower(c) ? (c - 'a' + 'A') : c); +} + +static __inline uint8_t +_citrus_bcs_tolower(uint8_t c) +{ + return (_citrus_bcs_isupper(c) ? (c - 'A' + 'a') : c); +} + +__BEGIN_DECLS +int _citrus_bcs_strcasecmp(const char * __restrict, const char * __restrict); +int _citrus_bcs_strncasecmp(const char * __restrict, const char * __restrict, + size_t); +const char *_citrus_bcs_skip_ws(const char * __restrict); +const char *_citrus_bcs_skip_nonws(const char * __restrict); +const char *_citrus_bcs_skip_ws_len(const char * __restrict, + size_t * __restrict); +const char *_citrus_bcs_skip_nonws_len(const char * __restrict, + size_t * __restrict); +void _citrus_bcs_trunc_rws_len(const char * __restrict, size_t * __restrict); +void _citrus_bcs_convert_to_lower(char *); +void _citrus_bcs_convert_to_upper(char *); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/citrus_csmapper.c b/lib/libc/citrus/citrus_csmapper.c new file mode 100644 index 0000000000..960f6955ed --- /dev/null +++ b/lib/libc/citrus/citrus_csmapper.c @@ -0,0 +1,388 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_csmapper.c,v 1.5 2004/12/30 05:01:50 christos Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_csmapper.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "un-namespace.h" + +#include "libc_private.h" + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_module.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_csmapper.h" +#include "citrus_pivot_file.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" +#include "citrus_lookup.h" + +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +static struct _citrus_mapper_area *maparea = NULL; + +#define CS_ALIAS _PATH_CSMAPPER "/charset.alias" +#define CS_PIVOT _PATH_CSMAPPER "/charset.pivot" + + +/* ---------------------------------------------------------------------- */ + +static int +get32(struct _region *r, uint32_t *rval) +{ + if (_region_size(r) != 4) + return EFTYPE; + + memcpy(rval, _region_head(r), 4); + *rval = be32toh(*rval); + + return 0; +} + +static int +open_subdb(struct _citrus_db **subdb, struct _citrus_db *db, const char *src) +{ + int ret; + struct _region r; + + ret = _db_lookup_by_s(db, src, &r, NULL); + if (ret) + return ret; + ret = _db_open(subdb, &r, _CITRUS_PIVOT_SUB_MAGIC, _db_hash_std, NULL); + if (ret) + return ret; + + return 0; +} + + +#define NO_SUCH_FILE EOPNOTSUPP +static int +find_best_pivot_pvdb(const char *src, const char *dst, char *pivot, + size_t pvlen, unsigned long *rnorm) +{ + int ret, num, i; + struct _region fr, r1, r2; + struct _citrus_db *db1, *db2, *db3; + char buf[LINE_MAX]; + unsigned long norm; + uint32_t val32; + + ret = _map_file(&fr, CS_PIVOT ".pvdb"); + if (ret) { + if (ret == ENOENT) + ret = NO_SUCH_FILE; + return ret; + } + ret = _db_open(&db1, &fr, _CITRUS_PIVOT_MAGIC, _db_hash_std, NULL); + if (ret) + goto quit1; + ret = open_subdb(&db2, db1, src); + if (ret) + goto quit2; + + num = _db_get_num_entries(db2); + *rnorm = ULONG_MAX; + for (i = 0; i < num; i++) { + /* iterate each pivot */ + ret = _db_get_entry(db2, i, &r1, &r2); + if (ret) + goto quit3; + /* r1:pivot name, r2:norm among src and pivot */ + ret = get32(&r2, &val32); + if (ret) + goto quit3; + norm = val32; + snprintf(buf, sizeof(buf), "%.*s", + (int)_region_size(&r1), (char *)_region_head(&r1)); + /* buf: pivot name */ + ret = open_subdb(&db3, db1, buf); + if (ret) + goto quit3; + ret = _db_lookup_by_s(db3, dst, &r2, NULL); + if (ret) + goto quit4; + /* r2: norm among pivot and dst */ + ret = get32(&r2, &val32); + if (ret) + goto quit4; + norm += val32; + /* judge minimum norm */ + if (norm < *rnorm) { + *rnorm = norm; + strlcpy(pivot, buf, pvlen); + } +quit4: + _db_close(db3); + if (ret) + goto quit3; + } +quit3: + _db_close(db2); +quit2: + _db_close(db1); +quit1: + _unmap_file(&fr); + if (ret) + return ret; + + if (*rnorm == ULONG_MAX) + return ENOENT; + + return 0; +} + +/* ---------------------------------------------------------------------- */ + +struct zone { + const char *begin, *end; +}; + +struct parse_arg { + char dst[PATH_MAX]; + unsigned long norm; +}; + +static int +parse_line(struct parse_arg *pa, struct _region *r) +{ + char buf[20]; + struct zone z1, z2; + size_t len; + + len = _region_size(r); + z1.begin = _bcs_skip_ws_len(_region_head(r), &len); + if (len == 0) + return EFTYPE; + z1.end = _bcs_skip_nonws_len(z1.begin, &len); + if (len == 0) + return EFTYPE; + z2.begin = _bcs_skip_ws_len(z1.end, &len); + if (len == 0) + return EFTYPE; + z2.end = _bcs_skip_nonws_len(z2.begin, &len); + + /* z1 : dst name, z2 : norm */ + snprintf(pa->dst, sizeof(pa->dst), + "%.*s", (int)(z1.end-z1.begin), z1.begin); + snprintf(buf, sizeof(buf), + "%.*s", (int)(z2.end-z2.begin), z2.begin); + pa->norm = strtoul(buf, NULL, 0); + + return 0; +} + +static int +find_dst(struct parse_arg *pasrc, const char *dst) +{ + int ret; + struct parse_arg padst; + struct _lookup *cl; + struct _region data; + + ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE); + if (ret) + return ret; + + ret = _lookup_seq_lookup(cl, pasrc->dst, &data); + while (ret == 0) { + ret = parse_line(&padst, &data); + if (ret) + break; + if (strcmp(dst, padst.dst) == 0) { + pasrc->norm += padst.norm; + break; + } + ret = _lookup_seq_next(cl, NULL, &data); + } + _lookup_seq_close(cl); + + return ret; +} + +static int +find_best_pivot_lookup(const char *src, const char *dst, char *pivot, + size_t pvlen, unsigned long *rnorm) +{ + int ret; + struct _lookup *cl; + struct _region data; + struct parse_arg pa; + unsigned long norm_min; + char pivot_min[PATH_MAX]; + + ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE); + if (ret) + return ret; + + norm_min = ULONG_MAX; + + /* find pivot code */ + ret = _lookup_seq_lookup(cl, src, &data); + while (ret == 0) { + ret = parse_line(&pa, &data); + if (ret) + break; + ret = find_dst(&pa, dst); + if (ret) + break; + if (pa.norm < norm_min) { + norm_min = pa.norm; + strlcpy(pivot_min, pa.dst, sizeof(pivot_min)); + } + ret = _lookup_seq_next(cl, NULL, &data); + } + _lookup_seq_close(cl); + + if (ret != ENOENT) + return ret; + if (norm_min == ULONG_MAX) + return ENOENT; + strlcpy(pivot, pivot_min, pvlen); + if (rnorm) + *rnorm = norm_min; + + return 0; +} + +static int +find_best_pivot(const char *src, const char *dst, char *pivot, size_t pvlen, + unsigned long *rnorm) +{ + int ret; + + ret = find_best_pivot_pvdb(src, dst, pivot, pvlen, rnorm); + if (ret == NO_SUCH_FILE) + ret = find_best_pivot_lookup(src, dst, pivot, pvlen, rnorm); + + return ret; +} + +static __inline int +open_serial_mapper(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict * __restrict rcm, + const char *src, const char *pivot, const char *dst) +{ + char buf[PATH_MAX]; + + snprintf(buf, sizeof(buf), "%s/%s,%s/%s", src, pivot, pivot, dst); + + return _mapper_open_direct(ma, rcm, "mapper_serial", buf); +} + +static struct _citrus_csmapper *csm_none = NULL; +static int +get_none(struct _citrus_mapper_area *__restrict ma, + struct _citrus_csmapper *__restrict *__restrict rcsm) +{ + int ret; + + if (__isthreaded) + _pthread_mutex_lock(&lock); + + if (csm_none) { + *rcsm = csm_none; + ret = 0; + goto quit; + } + + ret = _mapper_open_direct(ma, &csm_none, "mapper_none", ""); + if (ret) + goto quit; + _mapper_set_persistent(csm_none); + + *rcsm = csm_none; + ret = 0; +quit: + if (__isthreaded) + _pthread_mutex_unlock(&lock); + + return ret; +} + +int +_citrus_csmapper_open(struct _citrus_csmapper * __restrict * __restrict rcsm, + const char * __restrict src, const char * __restrict dst, + uint32_t flags, unsigned long *rnorm) +{ + int ret; + char buf1[PATH_MAX], buf2[PATH_MAX], key[PATH_MAX], pivot[PATH_MAX]; + const char *realsrc, *realdst; + unsigned long norm; + + ret = _citrus_mapper_create_area(&maparea, _PATH_CSMAPPER); + if (ret) + return ret; + + realsrc = _lookup_alias(CS_ALIAS, src, buf1, sizeof(buf1), + _LOOKUP_CASE_IGNORE); + realdst = _lookup_alias(CS_ALIAS, dst, buf2, sizeof(buf2), + _LOOKUP_CASE_IGNORE); + if (!strcmp(realsrc, realdst)) { + ret = get_none(maparea, rcsm); + if (ret == 0 && rnorm != NULL) + *rnorm = 0; + return ret; + } + + snprintf(key, sizeof(key), "%s/%s", realsrc, realdst); + + ret = _mapper_open(maparea, rcsm, key); + if (ret == 0) { + if (rnorm != NULL) + *rnorm = 0; + return 0; + } + if (ret != ENOENT || (flags & _CSMAPPER_F_PREVENT_PIVOT)!=0) + return ret; + + ret = find_best_pivot(realsrc, realdst, pivot, sizeof(pivot), &norm); + if (ret) + return ret; + + ret = open_serial_mapper(maparea, rcsm, realsrc, pivot, realdst); + if (ret == 0 && rnorm != NULL) + *rnorm = norm; + + return ret; +} diff --git a/lib/libc/citrus/citrus_csmapper.h b/lib/libc/citrus/citrus_csmapper.h new file mode 100644 index 0000000000..dd6c9412f0 --- /dev/null +++ b/lib/libc/citrus/citrus_csmapper.h @@ -0,0 +1,49 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_csmapper.h,v 1.1 2003/06/25 09:51:27 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_csmapper.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_CSMAPPER_H_ +#define _CITRUS_CSMAPPER_H + +#define _citrus_csmapper _citrus_mapper +#define _citrus_csmapper_close _citrus_mapper_close +#define _citrus_csmapper_convert _citrus_mapper_convert +#define _citrus_csmapper_init_state _citrus_mapper_init_state +#define _citrus_csmapper_get_state_size _citrus_mapper_get_state_size +#define _citrus_csmapper_get_src_max _citrus_mapper_get_src_max +#define _citrus_csmapper_get_dst_max _citrus_mapper_get_dst_max + +#define _CITRUS_CSMAPPER_F_PREVENT_PIVOT 0x00000001 +__BEGIN_DECLS +int _citrus_csmapper_open(struct _citrus_csmapper *__restrict *__restrict, + const char *__restrict, + const char *__restrict, uint32_t, + unsigned long *); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/citrus_ctype.c b/lib/libc/citrus/citrus_ctype.c new file mode 100644 index 0000000000..3c0b037b02 --- /dev/null +++ b/lib/libc/citrus/citrus_ctype.c @@ -0,0 +1,207 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_ctype.c,v 1.4 2003/03/05 20:18:15 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_ctype.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)1999, 2000, 2001, 2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_ctype_fallback.h" +#include "citrus_none.h" +#include _CITRUS_DEFAULT_CTYPE_HEADER + +_citrus_ctype_rec_t _citrus_ctype_default = { + &_CITRUS_DEFAULT_CTYPE_OPS, /* cc_ops */ + NULL, /* cc_closure */ + NULL /* cc_module */ +}; + +#ifdef _I18N_DYNAMIC + +static int _initctypemodule(_citrus_ctype_t, char const *, _citrus_module_t, + void *, size_t, size_t); + +static int +_initctypemodule(_citrus_ctype_t cc, char const *modname, + _citrus_module_t handle, void *variable, size_t lenvar, + size_t szpriv) +{ + int ret; + _citrus_ctype_getops_t getops; + + _DIAGASSERT(cc != NULL); + + cc->cc_module = handle; + + getops = (_citrus_ctype_getops_t)_citrus_find_getops(cc->cc_module, + modname, + "ctype"); + if (getops == NULL) + return (EINVAL); + + cc->cc_ops = malloc(sizeof(*cc->cc_ops)); + if (cc->cc_ops == NULL) + return (ENOMEM); + + ret = (*getops)(cc->cc_ops, sizeof(*cc->cc_ops), + _CITRUS_CTYPE_ABI_VERSION); + if (ret) + goto bad; + + /* If return ABI version is not expected, fixup it here*/ + switch (cc->cc_ops->co_abi_version) { + case 0x00000001: + cc->cc_ops->co_btowc = &_citrus_ctype_btowc_fallback; + cc->cc_ops->co_wctob = &_citrus_ctype_wctob_fallback; + /* FALLTHROUGH */ + case 0x00000002: + /* FALLTHROUGH */ + default: + break; + } + + /* validation check */ + if (cc->cc_ops->co_init == NULL || + cc->cc_ops->co_uninit == NULL || + cc->cc_ops->co_get_mb_cur_max == NULL || + cc->cc_ops->co_mblen == NULL || + cc->cc_ops->co_mbrlen == NULL || + cc->cc_ops->co_mbrtowc == NULL || + cc->cc_ops->co_mbsinit == NULL || + cc->cc_ops->co_mbsrtowcs == NULL || + cc->cc_ops->co_mbstowcs == NULL || + cc->cc_ops->co_mbtowc == NULL || + cc->cc_ops->co_wcrtomb == NULL || + cc->cc_ops->co_wcsrtombs == NULL || + cc->cc_ops->co_wcstombs == NULL || + cc->cc_ops->co_wctomb == NULL || + cc->cc_ops->co_btowc == NULL || + cc->cc_ops->co_wctob == NULL) + goto bad; + + /* init and get closure */ + ret = (*cc->cc_ops->co_init)( + &cc->cc_closure, variable, lenvar, szpriv); + if (ret) + goto bad; + + return (0); + +bad: + if (cc->cc_ops) + free(cc->cc_ops); + cc->cc_ops = NULL; + + return (ret); +} + +int +_citrus_ctype_open(_citrus_ctype_t *rcc, + char const *encname, void *variable, size_t lenvar, + size_t szpriv) +{ + int ret; + _citrus_module_t handle; + _citrus_ctype_t cc; + + _DIAGASSERT(encname != NULL); + _DIAGASSERT(!lenvar || variable!=NULL); + _DIAGASSERT(rcc != NULL); + + if (!strcmp(encname, _CITRUS_DEFAULT_CTYPE_NAME)) { + *rcc = &_citrus_ctype_default; + return (0); + } + ret = _citrus_load_module(&handle, encname); + if (ret) + return (ret); + + cc = calloc(1, sizeof(*cc)); + if (!cc) { + _citrus_unload_module(handle); + return (errno); + } + + ret = _initctypemodule(cc, encname, handle, variable, lenvar, szpriv); + if (ret) { + _citrus_unload_module(cc->cc_module); + free(cc); + return (ret); + } + + *rcc = cc; + + return (0); +} + +void +_citrus_ctype_close(_citrus_ctype_t cc) +{ + + _DIAGASSERT(cc != NULL); + + if (cc == &_citrus_ctype_default) + return; + (*cc->cc_ops->co_uninit)(cc->cc_closure); + free(cc->cc_ops); + _citrus_unload_module(cc->cc_module); + free(cc); +} + +#else +/* !_I18N_DYNAMIC */ + +int +/*ARGSUSED*/ +_citrus_ctype_open(_citrus_ctype_t *rcc, + char const *encname, void *variable, size_t lenvar, + size_t szpriv) +{ + if (!strcmp(encname, _CITRUS_DEFAULT_CTYPE_NAME)) { + *rcc = &_citrus_ctype_default; + return (0); + } + return (EINVAL); +} + +void +/*ARGSUSED*/ +_citrus_ctype_close(_citrus_ctype_t cc) +{ +} + +#endif diff --git a/lib/libc/citrus/citrus_ctype.h b/lib/libc/citrus/citrus_ctype.h new file mode 100644 index 0000000000..e5be6922ef --- /dev/null +++ b/lib/libc/citrus/citrus_ctype.h @@ -0,0 +1,171 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_ctype.h,v 1.2 2003/03/05 20:18:15 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_ctype.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#ifndef _CITRUS_CTYPE_H_ +#define _CITRUS_CTYPE_H_ + +#include "citrus_ctype_local.h" + +typedef struct _citrus_ctype_rec *_citrus_ctype_t; + +__BEGIN_DECLS +int _citrus_ctype_open(_citrus_ctype_t * __restrict, + char const * __restrict, void * __restrict, + size_t, size_t); +void _citrus_ctype_close(_citrus_ctype_t); +__END_DECLS + +static __inline unsigned +_citrus_ctype_get_mb_cur_max(_citrus_ctype_t cc) +{ + + _DIAGASSERT(cc && cc->cc_ops); + return (*cc->cc_ops->co_get_mb_cur_max)(cc->cc_closure); +} + +static __inline int +_citrus_ctype_mblen(_citrus_ctype_t cc, const char *s, size_t n, int *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mblen && nresult); + return (*cc->cc_ops->co_mblen)(cc->cc_closure, s, n, nresult); +} + +static __inline int +_citrus_ctype_mbrlen(_citrus_ctype_t cc, const char *s, size_t n, + void *pspriv, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbrlen && nresult); + return (*cc->cc_ops->co_mbrlen)(cc->cc_closure, s, n, pspriv, nresult); +} + +static __inline int +_citrus_ctype_mbrtowc(_citrus_ctype_t cc, wchar_t *pwc, const char *s, + size_t n, void *pspriv, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbrtowc); + return (*cc->cc_ops->co_mbrtowc)(cc->cc_closure, pwc, s, n, pspriv, + nresult); +} + +static __inline int +_citrus_ctype_mbsinit(_citrus_ctype_t cc, void const *pspriv, int *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbsinit && nresult); + return (*cc->cc_ops->co_mbsinit)(cc->cc_closure, pspriv, nresult); +} + +static __inline int +_citrus_ctype_mbsrtowcs(_citrus_ctype_t cc, wchar_t *pwcs, const char **s, + size_t n, void *pspriv, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbsrtowcs && nresult); + return (*cc->cc_ops->co_mbsrtowcs)(cc->cc_closure, pwcs, s, n, pspriv, + nresult); +} + +static __inline int +_citrus_ctype_mbstowcs(_citrus_ctype_t cc, wchar_t *pwcs, const char *s, + size_t n, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbstowcs && nresult); + return (*cc->cc_ops->co_mbstowcs)(cc->cc_closure, pwcs, s, n, nresult); +} + +static __inline int +_citrus_ctype_mbtowc(_citrus_ctype_t cc, wchar_t *pw, const char *s, size_t n, + int *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_mbtowc && nresult); + return (*cc->cc_ops->co_mbtowc)(cc->cc_closure, pw, s, n, nresult); +} + +static __inline int +_citrus_ctype_wcrtomb(_citrus_ctype_t cc, char *s, wchar_t wc, + void *pspriv, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_wcrtomb && nresult); + return (*cc->cc_ops->co_wcrtomb)(cc->cc_closure, s, wc, pspriv, + nresult); +} + +static __inline int +_citrus_ctype_wcsrtombs(_citrus_ctype_t cc, char *s, const wchar_t **ppwcs, + size_t n, void *pspriv, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_wcsrtombs && nresult); + return (*cc->cc_ops->co_wcsrtombs)(cc->cc_closure, s, ppwcs, n, + pspriv, nresult); +} + +static __inline int +_citrus_ctype_wcstombs(_citrus_ctype_t cc, char *s, const wchar_t *wcs, + size_t n, size_t *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_wcstombs && nresult); + return (*cc->cc_ops->co_wcstombs)(cc->cc_closure, s, wcs, n, nresult); +} + +static __inline int +_citrus_ctype_wctomb(_citrus_ctype_t cc, char *s, wchar_t wc, int *nresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_wctomb && nresult); + return (*cc->cc_ops->co_wctomb)(cc->cc_closure, s, wc, nresult); +} + +static __inline int +_citrus_ctype_btowc(_citrus_ctype_t cc, int c, wint_t *wcresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_btowc && wcresult); + return (*cc->cc_ops->co_btowc)(cc, c, wcresult); +} + +static __inline int +_citrus_ctype_wctob(_citrus_ctype_t cc, wint_t c, int *cresult) +{ + + _DIAGASSERT(cc && cc->cc_ops && cc->cc_ops->co_wctob && cresult); + return (*cc->cc_ops->co_wctob)(cc, c, cresult); +} + +extern _citrus_ctype_rec_t _citrus_ctype_default; + +#endif diff --git a/lib/libc/citrus/citrus_ctype_fallback.c b/lib/libc/citrus/citrus_ctype_fallback.c new file mode 100644 index 0000000000..dbbe9d5669 --- /dev/null +++ b/lib/libc/citrus/citrus_ctype_fallback.c @@ -0,0 +1,104 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_ctype_fallback.c,v 1.2 2003/06/27 14:52:25 yamt Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_ctype_fallback.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include + +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_ctype_fallback.h" + +/* + * for ABI version >= 0x00000002 + */ + +int +_citrus_ctype_btowc_fallback(_citrus_ctype_rec_t * __restrict cc, + int c, wint_t * __restrict wcresult) +{ + char mb; + /* + * what we need is _PRIVSIZE + * and we know that it's smaller than sizeof(mbstate_t). + */ + char pspriv[sizeof(mbstate_t)]; + wchar_t wc; + size_t nr; + int err; + + _DIAGASSERT(cc != NULL && cc->cc_closure != NULL); + + if (c == EOF) { + *wcresult = WEOF; + return 0; + } + + memset(&pspriv, 0, sizeof(pspriv)); + mb = (char)(unsigned)c; + err = _citrus_ctype_mbrtowc(cc, &wc, &mb, 1, (void *)&pspriv, &nr); + if (!err && (nr == 0 || nr == 1)) + *wcresult = wc; + else + *wcresult = WEOF; + + return 0; +} + +int +_citrus_ctype_wctob_fallback(_citrus_ctype_rec_t * __restrict cc, + wint_t wc, int * __restrict cresult) +{ + /* + * what we need is _PRIVSIZE + * and we know that it's smaller than sizeof(mbstate_t). + */ + char pspriv[sizeof(mbstate_t)]; + char buf[MB_LEN_MAX]; + size_t nr; + int err; + + _DIAGASSERT(cc != NULL && cc->cc_closure != NULL); + + if (wc == WEOF) { + *cresult = EOF; + return 0; + } + memset(&pspriv, 0, sizeof(pspriv)); + err = _citrus_ctype_wcrtomb(cc, buf, (wchar_t)wc, (void *)&pspriv, &nr); + if (!err && nr == 1) + *cresult = buf[0]; + else + *cresult = EOF; + + return 0; +} diff --git a/lib/libc/citrus/citrus_ctype_fallback.h b/lib/libc/citrus/citrus_ctype_fallback.h new file mode 100644 index 0000000000..fcdaa64d42 --- /dev/null +++ b/lib/libc/citrus/citrus_ctype_fallback.h @@ -0,0 +1,39 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_ctype_fallback.h,v 1.1 2003/03/05 20:18:15 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_ctype_fallback.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_CTYPE_FALLBACK_H_ +#define _CITRUS_CTYPE_FALLBACK_H_ + +/* fallback functions for 0x00000002 */ +int _citrus_ctype_btowc_fallback(_citrus_ctype_rec_t * __restrict, + int, wint_t * __restrict); +int _citrus_ctype_wctob_fallback(_citrus_ctype_rec_t * __restrict, + wint_t, int * __restrict); + +#endif diff --git a/lib/libc/citrus/citrus_ctype_local.h b/lib/libc/citrus/citrus_ctype_local.h new file mode 100644 index 0000000000..da5c332cc0 --- /dev/null +++ b/lib/libc/citrus/citrus_ctype_local.h @@ -0,0 +1,200 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_ctype_local.h,v 1.2 2003/03/05 20:18:15 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_ctype_local.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#ifndef _CITRUS_CTYPE_LOCAL_H_ +#define _CITRUS_CTYPE_LOCAL_H_ + +#define _CITRUS_CTYPE_GETOPS_FUNC_BASE(_n_) \ +int _n_(_citrus_ctype_ops_rec_t *, size_t, uint32_t) +#define _CITRUS_CTYPE_GETOPS_FUNC(_n_) \ +_CITRUS_CTYPE_GETOPS_FUNC_BASE(_citrus_##_n_##_ctype_getops) + +#define _CITRUS_CTYPE_DECLS(_e_) \ +static int _citrus_##_e_##_ctype_init \ + (void ** __restrict, void * __restrict, size_t, size_t); \ +static void _citrus_##_e_##_ctype_uninit(void *); \ +static unsigned _citrus_##_e_##_ctype_get_mb_cur_max(void *); \ +static int _citrus_##_e_##_ctype_mblen(void * __restrict, \ + const char * __restrict, \ + size_t, int * __restrict); \ +static int _citrus_##_e_##_ctype_mbrlen(void * __restrict, \ + const char * __restrict, \ + size_t, void * __restrict, \ + size_t * __restrict); \ +static int _citrus_##_e_##_ctype_mbrtowc(void * __restrict, \ + wchar_t * __restrict, \ + const char * __restrict, size_t, \ + void * __restrict, \ + size_t * __restrict); \ +static int _citrus_##_e_##_ctype_mbsinit(void * __restrict, \ + void const * __restrict, \ + int * __restrict); \ +static int _citrus_##_e_##_ctype_mbsrtowcs(void * __restrict, \ + wchar_t * __restrict, \ + const char ** __restrict, \ + size_t, void * __restrict, \ + size_t * __restrict); \ +static int _citrus_##_e_##_ctype_mbstowcs(void * __restrict, \ + wchar_t * __restrict, \ + const char * __restrict, \ + size_t, size_t * __restrict); \ +static int _citrus_##_e_##_ctype_mbtowc(void * __restrict, \ + wchar_t * __restrict, \ + const char * __restrict, \ + size_t, int * __restrict); \ +static int _citrus_##_e_##_ctype_wcrtomb(void * __restrict, \ + char * __restrict, wchar_t, \ + void * __restrict, \ + size_t * __restrict); \ +static int _citrus_##_e_##_ctype_wcsrtombs(void * __restrict, \ + char * __restrict, \ + const wchar_t ** __restrict, \ + size_t, void * __restrict, \ + size_t * __restrict); \ +static int _citrus_##_e_##_ctype_wcstombs(void * __restrict, \ + char * __restrict, \ + const wchar_t * __restrict, \ + size_t, size_t * __restrict); \ +static int _citrus_##_e_##_ctype_wctomb(void * __restrict, \ + char * __restrict, \ + wchar_t, int * __restrict); \ +static int _citrus_##_e_##_ctype_btowc(_citrus_ctype_rec_t * __restrict, \ + int, wint_t * __restrict); \ +static int _citrus_##_e_##_ctype_wctob(_citrus_ctype_rec_t * __restrict, \ + wint_t, int * __restrict) + +#define _CITRUS_CTYPE_DEF_OPS(_e_) \ +_citrus_ctype_ops_rec_t _citrus_##_e_##_ctype_ops = { \ + /* co_abi_version */ _CITRUS_CTYPE_ABI_VERSION, \ + /* co_init */ &_citrus_##_e_##_ctype_init, \ + /* co_uninit */ &_citrus_##_e_##_ctype_uninit, \ + /* co_get_mb_cur_max */ &_citrus_##_e_##_ctype_get_mb_cur_max, \ + /* co_mblen */ &_citrus_##_e_##_ctype_mblen, \ + /* co_mbrlen */ &_citrus_##_e_##_ctype_mbrlen, \ + /* co_mbrtowc */ &_citrus_##_e_##_ctype_mbrtowc, \ + /* co_mbsinit */ &_citrus_##_e_##_ctype_mbsinit, \ + /* co_mbsrtowcs */ &_citrus_##_e_##_ctype_mbsrtowcs, \ + /* co_mbstowcs */ &_citrus_##_e_##_ctype_mbstowcs, \ + /* co_mbtowc */ &_citrus_##_e_##_ctype_mbtowc, \ + /* co_wcrtomb */ &_citrus_##_e_##_ctype_wcrtomb, \ + /* co_wcsrtombs */ &_citrus_##_e_##_ctype_wcsrtombs, \ + /* co_wcstombs */ &_citrus_##_e_##_ctype_wcstombs, \ + /* co_wctomb */ &_citrus_##_e_##_ctype_wctomb, \ + /* co_btowc */ &_citrus_##_e_##_ctype_btowc, \ + /* co_wctob */ &_citrus_##_e_##_ctype_wctob \ +} + +typedef struct _citrus_ctype_ops_rec _citrus_ctype_ops_rec_t; +typedef struct _citrus_ctype_rec _citrus_ctype_rec_t; + +typedef int (*_citrus_ctype_init_t) + (void ** __restrict, void * __restrict, size_t, size_t); +typedef void (*_citrus_ctype_uninit_t)(void *); +typedef unsigned (*_citrus_ctype_get_mb_cur_max_t)(void *); +typedef int (*_citrus_ctype_mblen_t) + (void * __restrict, const char * __restrict, size_t, int * __restrict); +typedef int (*_citrus_ctype_mbrlen_t) + (void * __restrict, const char * __restrict, size_t, + void * __restrict, size_t * __restrict); +typedef int (*_citrus_ctype_mbrtowc_t) + (void * __restrict, wchar_t * __restrict, const char * __restrict, + size_t, void * __restrict, size_t * __restrict); +typedef int (*_citrus_ctype_mbsinit_t) + (void * __restrict, const void * __restrict, int * __restrict); +typedef int (*_citrus_ctype_mbsrtowcs_t) + (void * __restrict, wchar_t * __restrict, const char ** __restrict, + size_t, void * __restrict, + size_t * __restrict); +typedef int (*_citrus_ctype_mbstowcs_t) + (void * __restrict, wchar_t * __restrict, const char * __restrict, + size_t, size_t * __restrict); +typedef int (*_citrus_ctype_mbtowc_t) + (void * __restrict, wchar_t * __restrict, const char * __restrict, + size_t, int * __restrict); +typedef int (*_citrus_ctype_wcrtomb_t) + (void * __restrict, char * __restrict, wchar_t, void * __restrict, + size_t * __restrict); +typedef int (*_citrus_ctype_wcsrtombs_t) + (void * __restrict, char * __restrict, const wchar_t ** __restrict, + size_t, void * __restrict, size_t * __restrict); +typedef int (*_citrus_ctype_wcstombs_t) + (void * __restrict, char * __restrict, const wchar_t * __restrict, + size_t, size_t * __restrict); +typedef int (*_citrus_ctype_wctomb_t) + (void * __restrict, char * __restrict, wchar_t, int * __restrict); +typedef int (*_citrus_ctype_btowc_t) + (_citrus_ctype_rec_t * __restrict, int, wint_t * __restrict); +typedef int (*_citrus_ctype_wctob_t) + (_citrus_ctype_rec_t * __restrict, wint_t, int * __restrict); + +/* + * ABI Version change log: + * 0x00000001 + * initial version + * 0x00000002 + * ops record: btowc and wctob are added. + * ctype record: unchanged. + */ +#define _CITRUS_CTYPE_ABI_VERSION 0x00000002 +struct _citrus_ctype_ops_rec { + uint32_t co_abi_version; + /* version 0x00000001 */ + _citrus_ctype_init_t co_init; + _citrus_ctype_uninit_t co_uninit; + _citrus_ctype_get_mb_cur_max_t co_get_mb_cur_max; + _citrus_ctype_mblen_t co_mblen; + _citrus_ctype_mbrlen_t co_mbrlen; + _citrus_ctype_mbrtowc_t co_mbrtowc; + _citrus_ctype_mbsinit_t co_mbsinit; + _citrus_ctype_mbsrtowcs_t co_mbsrtowcs; + _citrus_ctype_mbstowcs_t co_mbstowcs; + _citrus_ctype_mbtowc_t co_mbtowc; + _citrus_ctype_wcrtomb_t co_wcrtomb; + _citrus_ctype_wcsrtombs_t co_wcsrtombs; + _citrus_ctype_wcstombs_t co_wcstombs; + _citrus_ctype_wctomb_t co_wctomb; + /* version 0x00000002 */ + _citrus_ctype_btowc_t co_btowc; + _citrus_ctype_wctob_t co_wctob; +}; + +#define _CITRUS_DEFAULT_CTYPE_NAME "NONE" +#define _CITRUS_DEFAULT_CTYPE_OPS _citrus_NONE_ctype_ops +#define _CITRUS_DEFAULT_CTYPE_HEADER "citrus_none.h" + +typedef _CITRUS_CTYPE_GETOPS_FUNC_BASE((*_citrus_ctype_getops_t)); +struct _citrus_ctype_rec { + _citrus_ctype_ops_rec_t *cc_ops; + void *cc_closure; + _citrus_module_t cc_module; +}; + +#endif diff --git a/lib/libc/citrus/citrus_ctype_template.h b/lib/libc/citrus/citrus_ctype_template.h new file mode 100644 index 0000000000..6ae064ceb3 --- /dev/null +++ b/lib/libc/citrus/citrus_ctype_template.h @@ -0,0 +1,769 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_ctype_template.h,v 1.32 2005/03/05 17:31:03 tnozaki Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_ctype_template.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + + +/* + * CAUTION: THIS IS NOT STANDALONE FILE + * + * function templates of ctype encoding handler for each encodings. + * + * you need to define the macros below: + * + * _FUNCNAME(method) : + * It should convine the real function name for the method. + * e.g. _FUNCNAME(mbrtowc) should be expanded to + * _EUC_ctype_mbrtowc + * for EUC locale. + * + * _CEI_TO_STATE(cei, method) : + * It should be expanded to the pointer of the method-internal state + * structures. + * e.g. _CEI_TO_STATE(cei, mbrtowc) might be expanded to + * (cei)->states.s_mbrtowc + * This structure may use if the function is called as + * mbrtowc(&wc, s, n, NULL); + * Such individual structures are needed by: + * mblen + * mbrlen + * mbrtowc + * mbtowc + * mbsrtowcs + * wcrtomb + * wcsrtombs + * wctomb + * These need to be keeped in the ctype encoding information structure, + * pointed by "cei". + * + * _ENCODING_INFO : + * It should be expanded to the name of the encoding information structure. + * e.g. For EUC encoding, this macro is expanded to _EUCInfo. + * Encoding information structure need to contain the common informations + * for the codeset. + * + * _ENCODING_STATE : + * It should be expanded to the name of the encoding state structure. + * e.g. For EUC encoding, this macro is expanded to _EUCState. + * Encoding state structure need to contain the context-dependent states, + * which are "unpacked-form" of mbstate_t type and keeped during sequent + * calls of mb/wc functions, + * + * _ENCODING_IS_STATE_DEPENDENT : + * If the encoding is state dependent, this should be expanded to + * non-zero integral value. Otherwise, 0. + * + * _STATE_NEEDS_EXPLICIT_INIT(ps) : + * some encodings, states needs some explicit initialization. + * (ie. initialization with memset isn't enough.) + * If the encoding state pointed by "ps" needs to be initialized + * explicitly, return non-zero. Otherwize, 0. + * + */ + + +/* prototypes */ + +__BEGIN_DECLS +static void _FUNCNAME(init_state)(_ENCODING_INFO * __restrict, + _ENCODING_STATE * __restrict); +static void _FUNCNAME(pack_state)(_ENCODING_INFO * __restrict, + void * __restrict, + const _ENCODING_STATE * __restrict); +static void _FUNCNAME(unpack_state)(_ENCODING_INFO * __restrict, + _ENCODING_STATE * __restrict, + const void * __restrict); +#if _ENCODING_IS_STATE_DEPENDENT +static int _FUNCNAME(put_state_reset)(_ENCODING_INFO * __restrict, + char * __restrict, size_t, + _ENCODING_STATE * __restrict, + size_t * __restrict); +#endif + +/* + * standard form of mbrtowc_priv. + * + * note (differences from real mbrtowc): + * - 3rd parameter is not "const char *s" but "const char **s". + * after the call of the function, *s will point the first byte of + * the next character. + * - additional 4th parameter is the size of src buffer. + * - 5th parameter is unpacked encoding-dependent state structure. + * - additional 6th parameter is the storage to be stored + * the return value in the real mbrtowc context. + * - return value means "errno" in the real mbrtowc context. + */ + +static int _FUNCNAME(mbrtowc_priv)(_ENCODING_INFO * __restrict, + wchar_t * __restrict, + const char ** __restrict, + size_t, _ENCODING_STATE * __restrict, + size_t * __restrict); + +/* + * standard form of wcrtomb_priv. + * + * note (differences from real wcrtomb): + * - additional 3th parameter is the size of src buffer. + * - 5th parameter is unpacked encoding-dependent state structure. + * - additional 6th parameter is the storage to be stored + * the return value in the real mbrtowc context. + * - return value means "errno" in the real wcrtomb context. + * - caller should ensure that 2nd parameter isn't NULL. + * (XXX inconsist with mbrtowc_priv) + */ + +static int _FUNCNAME(wcrtomb_priv)(_ENCODING_INFO * __restrict, + char * __restrict, size_t, wchar_t, + _ENCODING_STATE * __restrict, + size_t * __restrict); +__END_DECLS + + +/* + * macros + */ + +#define _TO_CEI(_cl_) ((_CTYPE_INFO*)(_cl_)) + + +/* + * templates + */ + +/* internal routines */ + +static __inline int +_FUNCNAME(mbtowc_priv)(_ENCODING_INFO * __restrict ei, + wchar_t * __restrict pwc, const char * __restrict s, + size_t n, _ENCODING_STATE * __restrict psenc, + int * __restrict nresult) +{ + _ENCODING_STATE state; + size_t nr; + int err = 0; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + + if (s == NULL) { + *nresult = _ENCODING_IS_STATE_DEPENDENT; + return (0); + } + + state = *psenc; + err = _FUNCNAME(mbrtowc_priv)(ei, pwc, (const char **)&s, n, psenc, &nr); + if (err) { + *nresult = -1; + return (err); + } + if (nr == (size_t)-2) { + *psenc = state; + *nresult = -1; + return (EILSEQ); + } + + *nresult = (int)nr; + + return (0); +} + +static int +_FUNCNAME(mbsrtowcs_priv)(_ENCODING_INFO * __restrict ei, + wchar_t * __restrict pwcs, + const char ** __restrict s, + size_t n, _ENCODING_STATE * __restrict psenc, + size_t * __restrict nresult) +{ + int err, cnt; + size_t siz; + const char *s0; + size_t mbcurmax; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s == NULL); + _DIAGASSERT(*s == NULL); + + /* if pwcs is NULL, ignore n */ + if (pwcs == NULL) + n = 1; /* arbitrary >0 value */ + + err = cnt = 0; + s0 = *s; /* to keep *s unchanged for now, use copy instead. */ + mbcurmax = _ENCODING_MB_CUR_MAX(ei); + while (n > 0) { + err = _FUNCNAME(mbrtowc_priv)(ei, pwcs, &s0, mbcurmax, + psenc, &siz); + if (siz == (size_t)-2) + err = EILSEQ; + if (err) { + cnt = -1; + goto bye; + } + switch (siz) { + case 0: + if (pwcs) { + _FUNCNAME(init_state)(ei, psenc); + } + s0 = 0; + goto bye; + default: + if (pwcs) { + pwcs++; + n--; + } + cnt++; + break; + } + } +bye: + if (pwcs) + *s = s0; + + *nresult = (size_t)cnt; + + return err; +} + + +static int +_FUNCNAME(wcsrtombs_priv)(_ENCODING_INFO * __restrict ei, char * __restrict s, + const wchar_t ** __restrict pwcs, + size_t n, _ENCODING_STATE * __restrict psenc, + size_t * __restrict nresult) +{ + int cnt = 0, err; + char buf[MB_LEN_MAX]; + size_t siz; + const wchar_t* pwcs0; +#if _ENCODING_IS_STATE_DEPENDENT + _ENCODING_STATE state; +#endif + + pwcs0 = *pwcs; + + if (!s) + n = 1; + + while (n > 0) { +#if _ENCODING_IS_STATE_DEPENDENT + state = *psenc; +#endif + err = _FUNCNAME(wcrtomb_priv)(ei, buf, sizeof(buf), + *pwcs0, psenc, &siz); + if (siz == (size_t)-1) { + *nresult = siz; + return (err); + } + + if (s) { + if (n < siz) { +#if _ENCODING_IS_STATE_DEPENDENT + *psenc = state; +#endif + break; + } + memcpy(s, buf, siz); + s += siz; + n -= siz; + } + cnt += siz; + if (!*pwcs0) { + if (s) { + _FUNCNAME(init_state)(ei, psenc); + } + pwcs0 = 0; + cnt--; /* don't include terminating null */ + break; + } + pwcs0++; + } + if (s) + *pwcs = pwcs0; + + *nresult = (size_t)cnt; + return (0); +} + + +/* ---------------------------------------------------------------------- + * templates for public functions + */ + +#define _RESTART_BEGIN(_func_, _cei_, _pspriv_, _pse_) \ +do { \ + _ENCODING_STATE _state; \ + do { \ + if (_pspriv_ == NULL) { \ + _pse_ = &_CEI_TO_STATE(_cei_, _func_); \ + if (_STATE_NEEDS_EXPLICIT_INIT(_pse_)) \ + _FUNCNAME(init_state)(_CEI_TO_EI(_cei_), \ + (_pse_)); \ + } else { \ + _pse_ = &_state; \ + _FUNCNAME(unpack_state)(_CEI_TO_EI(_cei_), \ + _pse_, _pspriv_); \ + } \ + } while (/*CONSTCOND*/0) + +#define _RESTART_END(_func_, _cei_, _pspriv_, _pse_) \ + if (_pspriv_ != NULL) { \ + _FUNCNAME(pack_state)(_CEI_TO_EI(_cei_), _pspriv_, \ + _pse_); \ + } \ +} while (/*CONSTCOND*/0) + +int +_FUNCNAME(ctype_getops)(_citrus_ctype_ops_rec_t *ops, size_t lenops, + uint32_t expected_version) +{ + if (expected_version<_CITRUS_CTYPE_ABI_VERSION || lenops lenps) + return (EINVAL); + + cei = calloc(1, sizeof(_CTYPE_INFO)); + if (cei == NULL) + return (ENOMEM); + + *cl = (void *)cei; + + return _FUNCNAME(encoding_module_init)(_CEI_TO_EI(cei), var, lenvar); +} + +static void +_FUNCNAME(ctype_uninit)(void *cl) +{ + if (cl) { + _FUNCNAME(encoding_module_uninit)(_CEI_TO_EI(_TO_CEI(cl))); + free(cl); + } +} + +static unsigned +/*ARGSUSED*/ +_FUNCNAME(ctype_get_mb_cur_max)(void *cl) +{ + return _ENCODING_MB_CUR_MAX(_CEI_TO_EI(_TO_CEI(cl))); +} + +static int +_FUNCNAME(ctype_mblen)(void * __restrict cl, + const char * __restrict s, size_t n, + int * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + + _DIAGASSERT(cl != NULL); + + psenc = &_CEI_TO_STATE(_TO_CEI(cl), mblen); + ei = _CEI_TO_EI(_TO_CEI(cl)); + if (_STATE_NEEDS_EXPLICIT_INIT(psenc)) + _FUNCNAME(init_state)(ei, psenc); + return _FUNCNAME(mbtowc_priv)(ei, NULL, s, n, psenc, nresult); +} + +static int +_FUNCNAME(ctype_mbrlen)(void * __restrict cl, const char * __restrict s, + size_t n, void * __restrict pspriv, + size_t * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + int err = 0; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _RESTART_BEGIN(mbrlen, _TO_CEI(cl), pspriv, psenc); + if (s == NULL) { + _FUNCNAME(init_state)(ei, psenc); + *nresult = 0; + } else { + err = _FUNCNAME(mbrtowc_priv)(ei, NULL, (const char **)&s, n, + (void *)psenc, nresult); + } + _RESTART_END(mbrlen, _TO_CEI(cl), pspriv, psenc); + + return (err); +} + +static int +_FUNCNAME(ctype_mbrtowc)(void * __restrict cl, wchar_t * __restrict pwc, + const char * __restrict s, size_t n, + void * __restrict pspriv, size_t * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + int err = 0; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _RESTART_BEGIN(mbrtowc, _TO_CEI(cl), pspriv, psenc); + if (s == NULL) { + _FUNCNAME(init_state)(ei, psenc); + *nresult = 0; + } else { + err = _FUNCNAME(mbrtowc_priv)(ei, pwc, (const char **)&s, n, + (void *)psenc, nresult); + } + _RESTART_END(mbrtowc, _TO_CEI(cl), pspriv, psenc); + + return (err); +} + +static int +/*ARGSUSED*/ +_FUNCNAME(ctype_mbsinit)(void * __restrict cl, const void * __restrict pspriv, + int * __restrict nresult) +{ + _ENCODING_STATE state; + + if (pspriv == NULL) { + *nresult = 1; + return (0); + } + + _FUNCNAME(unpack_state)(_CEI_TO_EI(_TO_CEI(cl)), &state, pspriv); + + *nresult = (state.chlen == 0); /* XXX: FIXME */ + + return (0); +} + +static int +_FUNCNAME(ctype_mbsrtowcs)(void * __restrict cl, wchar_t * __restrict pwcs, + const char ** __restrict s, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + int err = 0; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _RESTART_BEGIN(mbsrtowcs, _TO_CEI(cl), pspriv, psenc); + err = _FUNCNAME(mbsrtowcs_priv)(ei, pwcs, s, n, psenc, nresult); + _RESTART_END(mbsrtowcs, _TO_CEI(cl), pspriv, psenc); + + return (err); +} + +static int +_FUNCNAME(ctype_mbstowcs)(void * __restrict cl, wchar_t * __restrict pwcs, + const char * __restrict s, size_t n, + size_t * __restrict nresult) +{ + int err; + _ENCODING_STATE state; + _ENCODING_INFO *ei; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _FUNCNAME(init_state)(ei, &state); + err = _FUNCNAME(mbsrtowcs_priv)(ei, pwcs, (const char **)&s, n, + &state, nresult); + if (*nresult == (size_t)-2) { + err = EILSEQ; + *nresult = (size_t)-1; + } + + return (err); +} + +static int +_FUNCNAME(ctype_mbtowc)(void * __restrict cl, wchar_t * __restrict pwc, + const char * __restrict s, size_t n, + int * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + + _DIAGASSERT(cl != NULL); + + psenc = &_CEI_TO_STATE(_TO_CEI(cl), mbtowc); + ei = _CEI_TO_EI(_TO_CEI(cl)); + if (_STATE_NEEDS_EXPLICIT_INIT(psenc)) + _FUNCNAME(init_state)(ei, psenc); + return _FUNCNAME(mbtowc_priv)(ei, pwc, s, n, psenc, nresult); +} + +static int +_FUNCNAME(ctype_wcrtomb)(void * __restrict cl, char * __restrict s, wchar_t wc, + void * __restrict pspriv, size_t * __restrict nresult) +{ + _ENCODING_STATE *psenc; + char buf[MB_LEN_MAX]; + int err = 0; + size_t sz; +#if _ENCODING_IS_STATE_DEPENDENT + size_t rsz = 0; +#endif + + _DIAGASSERT(cl != NULL); + + if (s == NULL) { + /* + * use internal buffer. + */ + s = buf; + wc = L'\0'; /* SUSv3 */ + } + + _RESTART_BEGIN(wcrtomb, _TO_CEI(cl), pspriv, psenc); + sz = _ENCODING_MB_CUR_MAX(_CEI_TO_EI(_TO_CEI(cl))); +#if _ENCODING_IS_STATE_DEPENDENT + if (wc == L'\0') { + /* reset state */ + err = _FUNCNAME(put_state_reset)(_CEI_TO_EI(_TO_CEI(cl)), s, + sz, psenc, &rsz); + if (err) { + *nresult = -1; + goto quit; + } + s += rsz; + sz -= rsz; + } +#endif + err = _FUNCNAME(wcrtomb_priv)(_CEI_TO_EI(_TO_CEI(cl)), s, sz, + wc, psenc, nresult); +#if _ENCODING_IS_STATE_DEPENDENT + if (err == 0) + *nresult += rsz; +quit: +#endif + if (err == E2BIG) + err = EINVAL; + _RESTART_END(wcrtomb, _TO_CEI(cl), pspriv, psenc); + + return err; +} + +static int +/*ARGSUSED*/ +_FUNCNAME(ctype_wcsrtombs)(void * __restrict cl, char * __restrict s, + const wchar_t ** __restrict pwcs, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + int err = 0; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _RESTART_BEGIN(wcsrtombs, _TO_CEI(cl), pspriv, psenc); + err = _FUNCNAME(wcsrtombs_priv)(ei, s, pwcs, n, psenc, nresult); + _RESTART_END(wcsrtombs, _TO_CEI(cl), pspriv, psenc); + + return err; +} + +static int +/*ARGSUSED*/ +_FUNCNAME(ctype_wcstombs)(void * __restrict cl, char * __restrict s, + const wchar_t * __restrict pwcs, size_t n, + size_t * __restrict nresult) +{ + _ENCODING_STATE state; + _ENCODING_INFO *ei; + int err; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + _FUNCNAME(init_state)(ei, &state); + err = _FUNCNAME(wcsrtombs_priv)(ei, s, (const wchar_t **)&pwcs, n, + &state, nresult); + + return err; +} + +static int +_FUNCNAME(ctype_wctomb)(void * __restrict cl, char * __restrict s, wchar_t wc, + int * __restrict nresult) +{ + _ENCODING_STATE *psenc; + _ENCODING_INFO *ei; + size_t nr, sz; +#if _ENCODING_IS_STATE_DEPENDENT + size_t rsz = 0; +#endif + int err = 0; + + _DIAGASSERT(cl != NULL); + + ei = _CEI_TO_EI(_TO_CEI(cl)); + psenc = &_CEI_TO_STATE(_TO_CEI(cl), wctomb); + if (_STATE_NEEDS_EXPLICIT_INIT(psenc)) + _FUNCNAME(init_state)(ei, psenc); + if (s == NULL) { + _FUNCNAME(init_state)(ei, psenc); + *nresult = _ENCODING_IS_STATE_DEPENDENT; + return 0; + } + sz = _ENCODING_MB_CUR_MAX(_CEI_TO_EI(_TO_CEI(cl))); +#if _ENCODING_IS_STATE_DEPENDENT + if (wc == L'\0') { + /* reset state */ + err = _FUNCNAME(put_state_reset)(_CEI_TO_EI(_TO_CEI(cl)), s, + sz, psenc, &rsz); + if (err) { + *nresult = -1; /* XXX */ + return 0; + } + s += rsz; + sz -= rsz; + } +#endif + err = _FUNCNAME(wcrtomb_priv)(ei, s, sz, wc, psenc, &nr); +#if _ENCODING_IS_STATE_DEPENDENT + if (err == 0) + *nresult = (int)(nr + rsz); + else +#endif + *nresult = (int)nr; + + return 0; +} + +static int +/*ARGSUSED*/ +_FUNCNAME(ctype_btowc)(_citrus_ctype_rec_t * __restrict cc, + int c, wint_t * __restrict wcresult) +{ + _ENCODING_STATE state; + _ENCODING_INFO *ei; + char mb; + char const *s; + wchar_t wc; + size_t nr; + int err; + + _DIAGASSERT(cc != NULL && cc->cc_closure != NULL); + + if (c == EOF) { + *wcresult = WEOF; + return 0; + } + ei = _CEI_TO_EI(_TO_CEI(cc->cc_closure)); + _FUNCNAME(init_state)(ei, &state); + mb = (char)(unsigned)c; + s = &mb; + err = _FUNCNAME(mbrtowc_priv)(ei, &wc, &s, 1, &state, &nr); + if (!err && (nr == 0 || nr == 1)) + *wcresult = (wint_t)wc; + else + *wcresult = WEOF; + + return 0; +} + +static int +/*ARGSUSED*/ +_FUNCNAME(ctype_wctob)(_citrus_ctype_rec_t * __restrict cc, + wint_t wc, int * __restrict cresult) +{ + _ENCODING_STATE state; + _ENCODING_INFO *ei; + char buf[MB_LEN_MAX]; + size_t nr; + int err; + + _DIAGASSERT(cc != NULL && cc->cc_closure != NULL); + + if (wc == WEOF) { + *cresult = EOF; + return 0; + } + ei = _CEI_TO_EI(_TO_CEI(cc->cc_closure)); + _FUNCNAME(init_state)(ei, &state); + err = _FUNCNAME(wcrtomb_priv)(ei, buf, _ENCODING_MB_CUR_MAX(ei), + (wchar_t)wc, &state, &nr); + if (!err && nr == 1) + *cresult = buf[0]; + else + *cresult = EOF; + + return 0; +} diff --git a/lib/libc/citrus/citrus_db.c b/lib/libc/citrus/citrus_db.c new file mode 100644 index 0000000000..dbdd4de39f --- /dev/null +++ b/lib/libc/citrus/citrus_db.c @@ -0,0 +1,338 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_db.c,v 1.3 2004/01/02 21:49:35 itojun Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_db.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_db.h" +#include "citrus_db_file.h" + +struct _citrus_db { + /* private */ + struct _region db_region; + uint32_t (*db_hashfunc)(void *, struct _citrus_region *); + void *db_hashfunc_closure; +}; + +int +_citrus_db_open(struct _citrus_db **rdb, struct _region *r, const char *magic, + uint32_t (*hashfunc)(void *, struct _citrus_region *), + void *hashfunc_closure) +{ + struct _memstream ms; + struct _citrus_db *db; + struct _citrus_db_header_x *dhx; + + _memstream_bind(&ms, r); + + /* sanity check */ + dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx)); + if (dhx == NULL) + return EFTYPE; + if (strncmp(dhx->dhx_magic, magic, _CITRUS_DB_MAGIC_SIZE) != 0) + return EFTYPE; + if (_memstream_seek(&ms, be32toh(dhx->dhx_entry_offset), SEEK_SET)) + return EFTYPE; + + if (be32toh(dhx->dhx_num_entries)*_CITRUS_DB_ENTRY_SIZE > + _memstream_remainder(&ms)) + return EFTYPE; + + db = malloc(sizeof(*db)); + if (db==NULL) + return errno; + db->db_region = *r; + db->db_hashfunc = hashfunc; + db->db_hashfunc_closure = hashfunc_closure; + *rdb = db; + + return 0; +} + +void +_citrus_db_close(struct _citrus_db *db) +{ + free(db); +} + +int +_citrus_db_lookup(struct _citrus_db *db, struct _citrus_region *key, + struct _citrus_region *data, struct _citrus_db_locator *dl) +{ + uint32_t hashval, num_entries; + size_t offset; + struct _memstream ms; + struct _citrus_db_header_x *dhx; + struct _citrus_db_entry_x *dex; + struct _citrus_region r; + + _memstream_bind(&ms, &db->db_region); + + dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx)); + _DIAGASSERT(dhx); + num_entries = be32toh(dhx->dhx_num_entries); + if (num_entries == 0) + return ENOENT; + + if (dl != NULL && dl->dl_offset>0) { + hashval = dl->dl_hashval; + offset = dl->dl_offset; + if (offset >= _region_size(&db->db_region)) + return ENOENT; + } else { + hashval = + db->db_hashfunc(db->db_hashfunc_closure, key)%num_entries; + offset = + be32toh(dhx->dhx_entry_offset) + + hashval * _CITRUS_DB_ENTRY_SIZE; + if (dl) + dl->dl_hashval = hashval; + } + do { + /* seek to the next entry */ + if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET)) + return EFTYPE; + /* get the entry record */ + dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE); + if (dex == NULL) + return EFTYPE; + + /* jump to next entry having the same hash value. */ + offset = be32toh(dex->dex_next_offset); + + /* save the current position */ + if (dl) { + dl->dl_offset = offset; + if (offset==0) + dl->dl_offset = _region_size(&db->db_region); + } + + /* compare hash value. */ + if (be32toh(dex->dex_hash_value) != hashval) + /* not found */ + break; + /* compare key length */ + if (be32toh(dex->dex_key_size) == _region_size(key)) { + /* seek to the head of the key. */ + if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), + SEEK_SET)) + return EFTYPE; + /* get the region of the key */ + if (_memstream_getregion(&ms, &r, + _region_size(key)) == NULL) + return EFTYPE; + /* compare key byte stream */ + if (memcmp(_region_head(&r), _region_head(key), + _region_size(key)) == 0) { + /* match */ + if (_memstream_seek( + &ms, be32toh(dex->dex_data_offset), + SEEK_SET)) + return EFTYPE; + if (_memstream_getregion( + &ms, data, + be32toh(dex->dex_data_size)) == NULL) + return EFTYPE; + return 0; + } + } + } while (offset != 0); + + return ENOENT; +} + +int +_citrus_db_lookup_by_string(struct _citrus_db *db, const char *key, + struct _citrus_region *data, + struct _citrus_db_locator *dl) +{ + struct _region r; + + /* LINTED: discard const */ + _region_init(&r, (char *)key, strlen(key)); + + return _citrus_db_lookup(db, &r, data, dl); +} + +int +_citrus_db_lookup8_by_string(struct _citrus_db *db, const char *key, + uint8_t *rval, struct _citrus_db_locator *dl) +{ + int ret; + struct _region r; + + ret = _citrus_db_lookup_by_string(db, key, &r, dl); + if (ret) + return ret; + + if (_region_size(&r) != 1) + return EFTYPE; + + if (rval) + memcpy(rval, _region_head(&r), 1); + + return 0; +} + +int +_citrus_db_lookup16_by_string(struct _citrus_db *db, const char *key, + uint16_t *rval, struct _citrus_db_locator *dl) +{ + int ret; + struct _region r; + uint16_t val; + + ret = _citrus_db_lookup_by_string(db, key, &r, dl); + if (ret) + return ret; + + if (_region_size(&r) != 2) + return EFTYPE; + + if (rval) { + memcpy(&val, _region_head(&r), 2); + *rval = be16toh(val); + } + + return 0; +} + +int +_citrus_db_lookup32_by_string(struct _citrus_db *db, const char *key, + uint32_t *rval, struct _citrus_db_locator *dl) +{ + int ret; + struct _region r; + uint32_t val; + + ret = _citrus_db_lookup_by_string(db, key, &r, dl); + if (ret) + return ret; + + if (_region_size(&r) != 4) + return EFTYPE; + + if (rval) { + memcpy(&val, _region_head(&r), 4); + *rval = be32toh(val); + } + + return 0; +} + +int +_citrus_db_lookup_string_by_string(struct _citrus_db *db, const char *key, + const char **rdata, + struct _citrus_db_locator *dl) +{ + int ret; + struct _region r; + + ret = _citrus_db_lookup_by_string(db, key, &r, dl); + if (ret) + return ret; + + /* check whether the string is null terminated */ + if (_region_size(&r) == 0) + return EFTYPE; + if (*((const char*)_region_head(&r)+_region_size(&r)-1) != '\0') + return EFTYPE; + + if (rdata) + *rdata = _region_head(&r); + + return 0; +} + +int +_citrus_db_get_number_of_entries(struct _citrus_db *db) +{ + struct _memstream ms; + struct _citrus_db_header_x *dhx; + + _memstream_bind(&ms, &db->db_region); + + dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx)); + _DIAGASSERT(dhx); + return (int)be32toh(dhx->dhx_num_entries); +} + +int +_citrus_db_get_entry(struct _citrus_db *db, int idx, + struct _region *key, struct _region *data) +{ + uint32_t num_entries; + size_t offset; + struct _memstream ms; + struct _citrus_db_header_x *dhx; + struct _citrus_db_entry_x *dex; + + _memstream_bind(&ms, &db->db_region); + + dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx)); + _DIAGASSERT(dhx); + num_entries = be32toh(dhx->dhx_num_entries); + if (idx < 0 || (uint32_t)idx >= num_entries) + return EINVAL; + + /* seek to the next entry */ + offset = be32toh(dhx->dhx_entry_offset) + idx * _CITRUS_DB_ENTRY_SIZE; + if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET)) + return EFTYPE; + /* get the entry record */ + dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE); + if (dex == NULL) + return EFTYPE; + /* seek to the head of the key. */ + if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), SEEK_SET)) + return EFTYPE; + /* get the region of the key. */ + if (_memstream_getregion(&ms, key, be32toh(dex->dex_key_size))==NULL) + return EFTYPE; + /* seek to the head of the data. */ + if (_memstream_seek(&ms, be32toh(dex->dex_data_offset), SEEK_SET)) + return EFTYPE; + /* get the region of the data. */ + if (_memstream_getregion(&ms, data, be32toh(dex->dex_data_size))==NULL) + return EFTYPE; + + return 0; +} diff --git a/lib/libc/citrus/citrus_db.h b/lib/libc/citrus/citrus_db.h new file mode 100644 index 0000000000..c2c9b26925 --- /dev/null +++ b/lib/libc/citrus/citrus_db.h @@ -0,0 +1,76 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_db.h,v 1.1 2003/06/25 09:51:29 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_db.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_DB_H_ +#define _CITRUS_DB_H_ + +struct _citrus_db; +struct _citrus_db_locator { + u_int32_t dl_hashval; + size_t dl_offset; +}; + +__BEGIN_DECLS +int _citrus_db_open(struct _citrus_db **, struct _citrus_region *, + const char *, + u_int32_t (*)(void *, struct _citrus_region *), + void *); +void _citrus_db_close(struct _citrus_db *); +int _citrus_db_lookup(struct _citrus_db *, struct _citrus_region *, + struct _citrus_region *, + struct _citrus_db_locator *); +int _citrus_db_lookup_by_string(struct _citrus_db *, const char *, + struct _citrus_region *, + struct _citrus_db_locator *); +int _citrus_db_lookup8_by_string(struct _citrus_db *, const char *, + u_int8_t *, + struct _citrus_db_locator *); +int _citrus_db_lookup16_by_string(struct _citrus_db *, const char *, + u_int16_t *, + struct _citrus_db_locator *); +int _citrus_db_lookup32_by_string(struct _citrus_db *, const char *, + u_int32_t *, + struct _citrus_db_locator *); +int _citrus_db_lookup_string_by_string(struct _citrus_db *, const char *, + const char **, + struct _citrus_db_locator *); +int _citrus_db_get_number_of_entries(struct _citrus_db *); +int _citrus_db_get_entry(struct _citrus_db *, int, + struct _citrus_region *, struct _citrus_region *); +__END_DECLS + +static __inline void +_citrus_db_locator_init(struct _citrus_db_locator *dl) +{ + dl->dl_hashval = 0; + dl->dl_offset = 0; +} + +#endif diff --git a/lib/libc/citrus/citrus_db_factory.c b/lib/libc/citrus/citrus_db_factory.c new file mode 100644 index 0000000000..21400aa5a4 --- /dev/null +++ b/lib/libc/citrus/citrus_db_factory.c @@ -0,0 +1,345 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_db_factory.c,v 1.8 2004/01/02 21:49:35 itojun Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_db_factory.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_region.h" +#include "citrus_db_file.h" +#include "citrus_db_factory.h" + +struct _citrus_db_factory_entry { + STAILQ_ENTRY(_citrus_db_factory_entry) de_entry; + struct _citrus_db_factory_entry *de_next; + uint32_t de_hashvalue; + struct _region de_key; + int de_key_free; + struct _region de_data; + int de_data_free; + int de_idx; +}; + +struct _citrus_db_factory { + size_t df_num_entries; + STAILQ_HEAD(, _citrus_db_factory_entry) df_entries; + size_t df_total_key_size; + size_t df_total_data_size; + uint32_t (*df_hashfunc)(void *, struct _citrus_region *); + void *df_hashfunc_closure; +}; + +#define DB_ALIGN 16 + +int +_citrus_db_factory_create(struct _citrus_db_factory **rdf, + _citrus_db_hash_func_t hashfunc, + void *hashfunc_closure) +{ + struct _citrus_db_factory *df; + + df = malloc(sizeof(*df)); + if (df == NULL) + return errno; + df->df_num_entries = 0; + df->df_total_key_size = df->df_total_data_size = 0; + STAILQ_INIT(&df->df_entries); + df->df_hashfunc = hashfunc; + df->df_hashfunc_closure = hashfunc_closure; + + *rdf = df; + + return 0; +} + +void +_citrus_db_factory_free(struct _citrus_db_factory *df) +{ + struct _citrus_db_factory_entry *de; + + while ((de = STAILQ_FIRST(&df->df_entries)) != NULL) { + STAILQ_REMOVE_HEAD(&df->df_entries, de_entry); + if (de->de_key_free) + free(_region_head(&de->de_key)); + if (de->de_data_free) + free(_region_head(&de->de_data)); + free(de); + } + free(df); +} + +static __inline size_t +ceilto(size_t sz) +{ + return (sz+DB_ALIGN-1) & ~(DB_ALIGN-1); +} + +int +_citrus_db_factory_add(struct _citrus_db_factory *df, + struct _region *key, int keyfree, + struct _region *data, int datafree) +{ + struct _citrus_db_factory_entry *de; + + de = malloc(sizeof(*de)); + if (de == NULL) + return -1; + + de->de_hashvalue = df->df_hashfunc(df->df_hashfunc_closure, key); + de->de_key = *key; + de->de_key_free = keyfree; + de->de_data = *data; + de->de_data_free = datafree; + de->de_idx = -1; + + STAILQ_INSERT_TAIL(&df->df_entries, de, de_entry); + df->df_total_key_size += _region_size(key); + df->df_total_data_size += ceilto(_region_size(data)); + df->df_num_entries++; + + return 0; + +} + +int +_citrus_db_factory_add_by_string(struct _citrus_db_factory *df, + const char *key, + struct _citrus_region *data, int datafree) +{ + struct _region r; + char *tmp; + tmp = strdup(key); + if (tmp == NULL) + return errno; + _region_init(&r, tmp, strlen(key)); + return _citrus_db_factory_add(df, &r, 1, data, datafree); +} + +int +_citrus_db_factory_add8_by_string(struct _citrus_db_factory *df, + const char *key, uint8_t val) +{ + struct _region r; + uint8_t *p; + + p = malloc(sizeof(*p)); + if (p == NULL) + return errno; + *p = val; + _region_init(&r, p, 1); + return _citrus_db_factory_add_by_string(df, key, &r, 1); +} + +int +_citrus_db_factory_add16_by_string(struct _citrus_db_factory *df, + const char *key, uint16_t val) +{ + struct _region r; + uint16_t *p; + + p = malloc(sizeof(*p)); + if (p == NULL) + return errno; + *p = htons(val); + _region_init(&r, p, 2); + return _citrus_db_factory_add_by_string(df, key, &r, 1); +} + +int +_citrus_db_factory_add32_by_string(struct _citrus_db_factory *df, + const char *key, uint32_t val) +{ + struct _region r; + uint32_t *p; + + p = malloc(sizeof(*p)); + if (p == NULL) + return errno; + *p = htonl(val); + _region_init(&r, p, 4); + return _citrus_db_factory_add_by_string(df, key, &r, 1); +} + +int +_citrus_db_factory_add_string_by_string(struct _citrus_db_factory *df, + const char *key, const char *data) +{ + char *p; + struct _region r; + + p = strdup(data); + if (p == NULL) + return errno; + _region_init(&r, p, strlen(p)+1); + return _citrus_db_factory_add_by_string(df, key, &r, 1); +} + +size_t +_citrus_db_factory_calc_size(struct _citrus_db_factory *df) +{ + size_t sz; + + sz = ceilto(_CITRUS_DB_HEADER_SIZE); + sz += ceilto(_CITRUS_DB_ENTRY_SIZE * df->df_num_entries); + sz += ceilto(df->df_total_key_size); + sz += df->df_total_data_size; + + return sz; +} + +static __inline void +put8(struct _region *r, size_t *rofs, uint8_t val) +{ + *(uint8_t *)_region_offset(r, *rofs) = val; + *rofs += 1; +} + +static __inline void +put16(struct _region *r, size_t *rofs, uint16_t val) +{ + val = htons(val); + memcpy(_region_offset(r, *rofs), &val, 2); + *rofs += 2; +} + +static __inline void +put32(struct _region *r, size_t *rofs, uint32_t val) +{ + val = htonl(val); + memcpy(_region_offset(r, *rofs), &val, 4); + *rofs += 4; +} + +static __inline void +putpad(struct _region *r, size_t *rofs) +{ + size_t i; + for (i = ceilto(*rofs) - *rofs; i > 0; i--) + put8(r, rofs, 0); +} + +static __inline void +dump_header(struct _region *r, const char *magic, size_t *rofs, + size_t num_entries) +{ + while (*rofs<_CITRUS_DB_MAGIC_SIZE) + put8(r, rofs, *magic++); + put32(r, rofs, num_entries); + put32(r, rofs, _CITRUS_DB_HEADER_SIZE); +} + +int +_citrus_db_factory_serialize(struct _citrus_db_factory *df, const char *magic, + struct _region *r) +{ + size_t i, ofs, keyofs, dataofs, nextofs; + struct _citrus_db_factory_entry *de, **depp, *det; + + ofs = 0; + /* check whether more than 0 entries exist */ + if (df->df_num_entries == 0) { + dump_header(r, magic, &ofs, 0); + return 0; + } + /* allocate hash table */ + depp = malloc(sizeof(*depp) * df->df_num_entries); + if (depp == NULL) + return -1; + for (i = 0; i < df->df_num_entries; i++) + depp[i] = NULL; + + /* step1: store the entries which are not conflicting */ + STAILQ_FOREACH(de, &df->df_entries, de_entry) { + de->de_hashvalue %= df->df_num_entries; + de->de_idx = -1; + de->de_next = NULL; + if (depp[de->de_hashvalue] == NULL) { + depp[de->de_hashvalue] = de; + de->de_idx = (int)de->de_hashvalue; + } + } + + /* step2: resolve conflicts */ + i = 0; + STAILQ_FOREACH(de, &df->df_entries, de_entry) { + if (de->de_idx == -1) { + det = depp[de->de_hashvalue]; + while (det->de_next != NULL) + det = det->de_next; + det->de_next = de; + while (depp[i] != NULL) + i++; + depp[i] = de; + de->de_idx = (int)i; + } + } + + keyofs = + _CITRUS_DB_HEADER_SIZE + + ceilto(df->df_num_entries*_CITRUS_DB_ENTRY_SIZE); + dataofs = keyofs + ceilto(df->df_total_key_size); + + /* dump header */ + dump_header(r, magic, &ofs, df->df_num_entries); + + /* dump entries */ + for (i = 0; i < df->df_num_entries; i++) { + de = depp[i]; + nextofs = 0; + if (de->de_next) { + nextofs = + _CITRUS_DB_HEADER_SIZE + + de->de_next->de_idx * _CITRUS_DB_ENTRY_SIZE; + } + put32(r, &ofs, de->de_hashvalue); + put32(r, &ofs, nextofs); + put32(r, &ofs, keyofs); + put32(r, &ofs, _region_size(&de->de_key)); + put32(r, &ofs, dataofs); + put32(r, &ofs, _region_size(&de->de_data)); + memcpy(_region_offset(r, keyofs), + _region_head(&de->de_key), _region_size(&de->de_key)); + keyofs += _region_size(&de->de_key); + memcpy(_region_offset(r, dataofs), + _region_head(&de->de_data), _region_size(&de->de_data)); + dataofs += _region_size(&de->de_data); + putpad(r, &dataofs); + } + putpad(r, &ofs); + putpad(r, &keyofs); + free(depp); + + return 0; +} diff --git a/lib/libc/citrus/citrus_db_factory.h b/lib/libc/citrus/citrus_db_factory.h new file mode 100644 index 0000000000..66a78ec678 --- /dev/null +++ b/lib/libc/citrus/citrus_db_factory.h @@ -0,0 +1,60 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_db_factory.h,v 1.1 2003/06/25 09:51:30 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_db_factory.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_DB_FACTORY_H_ +#define _CITRUS_DB_FACTORY_H_ + +struct _citrus_db_factory; +typedef uint32_t (*_citrus_db_hash_func_t)(void *, struct _citrus_region *);; + +__BEGIN_DECLS +int _citrus_db_factory_create(struct _citrus_db_factory **, + _citrus_db_hash_func_t, void *); +void _citrus_db_factory_free(struct _citrus_db_factory *); +int _citrus_db_factory_add(struct _citrus_db_factory *, + struct _citrus_region *, int, + struct _citrus_region *, int); +int _citrus_db_factory_add_by_string(struct _citrus_db_factory *, + const char *, + struct _citrus_region *, int); +int _citrus_db_factory_add8_by_string(struct _citrus_db_factory *, + const char *, uint8_t); +int _citrus_db_factory_add16_by_string(struct _citrus_db_factory *, + const char *, uint16_t); +int _citrus_db_factory_add32_by_string(struct _citrus_db_factory *, + const char *, uint32_t); +int _citrus_db_factory_add_string_by_string(struct _citrus_db_factory *, + const char *, const char *); +size_t _citrus_db_factory_calc_size(struct _citrus_db_factory *); +int _citrus_db_factory_serialize(struct _citrus_db_factory *, + const char *, struct _citrus_region *); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/citrus_db_file.h b/lib/libc/citrus/citrus_db_file.h new file mode 100644 index 0000000000..017f241ea6 --- /dev/null +++ b/lib/libc/citrus/citrus_db_file.h @@ -0,0 +1,86 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_db_file.h,v 1.2 2003/06/30 17:54:13 christos Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_db_file.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_DB_FILE_H_ +#define _CITRUS_DB_FILE_H_ + +/* + * db format: + * +--- + * | header + * | - magic + * | - num entries + * +--- + * | entry directory + * | +------------ + * | | entry0 + * | | - hash value + * | | - next entry + * | | - key offset + * | | - key len + * | | - data offset + * | | - data size + * | |--- + * | | entry1 + * | | .. + * | | entryN + * | +--- + * +--- + * | key table + * | - key0 + * | ... + * | - keyN + * +--- + * | data table + * | - data0 + * | ... + * | - dataN + * +--- + */ + +#define _CITRUS_DB_MAGIC_SIZE 8 +#define _CITRUS_DB_HEADER_SIZE 16 +struct _citrus_db_header_x { + char dhx_magic[_CITRUS_DB_MAGIC_SIZE]; + uint32_t dhx_num_entries; + uint32_t dhx_entry_offset; +} __attribute__((__packed__)); + +struct _citrus_db_entry_x { + uint32_t dex_hash_value; + uint32_t dex_next_offset; + uint32_t dex_key_offset; + uint32_t dex_key_size; + uint32_t dex_data_offset; + uint32_t dex_data_size; +} __attribute__((__packed__)); +#define _CITRUS_DB_ENTRY_SIZE 24 + +#endif diff --git a/lib/libc/citrus/citrus_db_hash.c b/lib/libc/citrus/citrus_db_hash.c new file mode 100644 index 0000000000..b50236b5e3 --- /dev/null +++ b/lib/libc/citrus/citrus_db_hash.c @@ -0,0 +1,61 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_db_hash.c,v 1.4 2004/01/02 21:49:35 itojun Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_db_hash.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_db_hash.h" + +uint32_t +/*ARGSUSED*/ +_citrus_db_hash_std(void *closure __unused, struct _region *r) +{ + const uint8_t *p = _region_head(r); + uint32_t hash = 0, tmp; + size_t i; + + for (i = _region_size(r); i > 0; i--) { + hash <<= 4; + hash += _bcs_tolower(*p); + tmp = hash & 0xF0000000; + if (tmp != 0) { + hash ^= tmp; + hash ^= tmp >> 24; + } + p++; + } + return hash; +} diff --git a/lib/libc/citrus/citrus_db_hash.h b/lib/libc/citrus/citrus_db_hash.h new file mode 100644 index 0000000000..6f4c6c461a --- /dev/null +++ b/lib/libc/citrus/citrus_db_hash.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_db_hash.h,v 1.1 2003/06/25 09:51:31 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_db_hash.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_DB_HASH_H_ +#define _CITRUS_DB_HASH_H_ + +__BEGIN_DECLS +uint32_t _citrus_db_hash_std(void *, struct _citrus_region *); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/citrus_esdb.c b/lib/libc/citrus/citrus_esdb.c new file mode 100644 index 0000000000..76eaf53ca1 --- /dev/null +++ b/lib/libc/citrus/citrus_esdb.c @@ -0,0 +1,351 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_esdb.c,v 1.4 2004/07/21 14:16:34 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_esdb.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_lookup.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" +#include "citrus_esdb.h" +#include "citrus_esdb_file.h" + +#define ESDB_DIR "esdb.dir" +#define ESDB_ALIAS "esdb.alias" + +/* + * _citrus_esdb_alias: + * resolve encoding scheme name aliases. + */ +const char * +_citrus_esdb_alias(const char *esname, char *buf, size_t bufsize) +{ + return _lookup_alias(_PATH_ESDB "/" ESDB_ALIAS, esname, buf, bufsize, + _LOOKUP_CASE_IGNORE); +} + + +/* + * conv_esdb: + * external representation -> local structure. + */ +static int +conv_esdb(struct _citrus_esdb *esdb, struct _region *fr) +{ + int ret; + struct _citrus_db *db; + uint32_t version, num_charsets, csid, i, tmp; + char buf[100]; + const char *str; + + /* open db */ + ret = _db_open(&db, fr, _CITRUS_ESDB_MAGIC, &_db_hash_std, NULL); + if (ret) + goto err0; + + /* check version */ + ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_VERSION, &version, NULL); + if (ret) + goto err1; + switch (version) { + case 0x00000001: + /* current version */ + /* initial version */ + break; + default: + ret = EFTYPE; + goto err1; + } + + /* get encoding/variable */ + ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_ENCODING, &str, NULL); + if (ret) + goto err1; + esdb->db_encname = strdup(str); + if (esdb->db_encname == NULL) { + ret = errno; + goto err1; + } + + esdb->db_len_variable = 0; + esdb->db_variable = NULL; + ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_VARIABLE, &str, NULL); + if (ret == 0) { + esdb->db_len_variable = strlen(str)+1; + esdb->db_variable = strdup(str); + if (esdb->db_variable == NULL) { + ret = errno; + goto err2; + } + } else if (ret != ENOENT) + goto err2; + + /* get number of charsets */ + ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_NUM_CHARSETS, + &num_charsets, NULL); + if (ret) + goto err3; + esdb->db_num_charsets = num_charsets; + + /* get invalid character */ + ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_INVALID, &tmp, NULL); + if (ret == 0) { + esdb->db_use_invalid = 1; + esdb->db_invalid = tmp; + } else if (ret == ENOENT) + esdb->db_use_invalid = 0; + else + goto err3; + + /* get charsets */ + esdb->db_charsets = malloc(num_charsets * sizeof(*esdb->db_charsets)); + if (esdb->db_charsets == NULL) { + ret = errno; + goto err3; + } + for (i = 0; i < num_charsets; i++) { + snprintf(buf, sizeof(buf), + _CITRUS_ESDB_SYM_CSID_PREFIX "%d", i); + ret = _db_lookup32_by_s(db, buf, &csid, NULL); + if (ret) + goto err4; + esdb->db_charsets[i].ec_csid = csid; + + snprintf(buf, sizeof(buf), + _CITRUS_ESDB_SYM_CSNAME_PREFIX "%d", i); + ret = _db_lookupstr_by_s(db, buf, &str, NULL); + if (ret) + goto err4; + esdb->db_charsets[i].ec_csname = strdup(str); + if (esdb->db_charsets[i].ec_csname == NULL) { + ret = errno; + goto err4; + } + } + + _db_close(db); + return 0; + +err4: + for (; i > 0; i--) + free(esdb->db_charsets[i - 1].ec_csname); + free(esdb->db_charsets); +err3: + free(esdb->db_variable); +err2: + free(esdb->db_encname); +err1: + _db_close(db); + if (ret == ENOENT) + ret = EFTYPE; +err0: + return ret; +} + +/* + * _citrus_esdb_open: + * open an ESDB file. + */ +int +_citrus_esdb_open(struct _citrus_esdb *db, const char *esname) +{ + int ret; + const char *realname, *encfile; + char buf1[PATH_MAX], buf2[PATH_MAX], path[PATH_MAX]; + struct _region fr; + + _DIAGASSERT(esname != NULL); + + snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_ALIAS); + realname = _lookup_alias(path, esname, buf1, sizeof(buf1), + _LOOKUP_CASE_IGNORE); + + snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_DIR); + encfile = _lookup_simple(path, realname, buf2, sizeof(buf2), + _LOOKUP_CASE_IGNORE); + if (encfile==NULL) + return ENOENT; + + /* open file */ + snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, encfile); + ret = _map_file(&fr, path); + if (ret) + return ret; + + ret = conv_esdb(db, &fr); + + _unmap_file(&fr); + + return ret; +} + +/* + * _citrus_esdb_close: + * free an ESDB. + */ +void +_citrus_esdb_close(struct _citrus_esdb *db) +{ + int i; + + _DIAGASSERT(db != NULL); + _DIAGASSERT(db->db_num_charsets == 0 || db->db_charsets != NULL); + + for (i = 0; i < db->db_num_charsets; i++) + free(db->db_charsets[i].ec_csname); + db->db_num_charsets = 0; + free(db->db_charsets); db->db_charsets = NULL; + free(db->db_encname); db->db_encname = NULL; + db->db_len_variable = 0; + free(db->db_variable); db->db_variable = NULL; +} + +/* + * _citrus_esdb_free_list: + * free the list. + */ +void +_citrus_esdb_free_list(char **list, size_t num) +{ + size_t i; + + for (i = 0; i < num; i++) + free(list[i]); + free(list); +} + +/* + * _citrus_esdb_get_list: + * get esdb entries. + */ +int +_citrus_esdb_get_list(char ***rlist, size_t *rnum) +{ + int ret; + struct _region key; + size_t num; + struct _citrus_lookup *cla, *cld; + char **list, **q; + char buf[PATH_MAX]; + + num = 0; + + ret = _lookup_seq_open(&cla, _PATH_ESDB "/" ESDB_ALIAS, + _LOOKUP_CASE_IGNORE); + if (ret) + goto quit0; + + ret = _lookup_seq_open(&cld, _PATH_ESDB "/" ESDB_DIR, + _LOOKUP_CASE_IGNORE); + if (ret) + goto quit1; + + /* count number of entries */ + num = _lookup_get_num_entries(cla) + _lookup_get_num_entries(cld); + + _lookup_seq_rewind(cla); + _lookup_seq_rewind(cld); + + /* allocate list pointer space */ + list = malloc(num * sizeof(char *)); + num = 0; + if (list == NULL) { + ret = errno; + goto quit3; + } + + /* get alias entries */ + while ((ret = _lookup_seq_next(cla, &key, NULL)) == 0) { + snprintf(buf, sizeof(buf), "%.*s", + (int)_region_size(&key), + (const char *)_region_head(&key)); + _bcs_convert_to_lower(buf); + list[num] = strdup(buf); + if (list[num] == NULL) { + ret = errno; + goto quit3; + } + num++; + } + if (ret != ENOENT) + goto quit3; + /* get dir entries */ + while ((ret = _lookup_seq_next(cld, &key, NULL)) == 0) { + /* check duplicated entry */ + snprintf(buf, sizeof(buf), "%.*s", + (int)_region_size(&key), + (const char *)_region_head(&key)); + _bcs_convert_to_lower(buf); + ret = _lookup_seq_lookup(cla, buf, NULL); + if (ret) { + if (ret != ENOENT) + goto quit3; + /* not duplicated */ + list[num] = strdup(buf); + if (list[num] == NULL) { + ret = errno; + goto quit3; + } + num++; + } + } + if (ret != ENOENT) + goto quit3; + + ret = 0; + q = realloc(list, num * sizeof(char *)); + if (!q) { + ret = ENOMEM; + goto quit3; + } + list = q; + *rlist = list; + *rnum = num; +quit3: + if (ret) + _citrus_esdb_free_list(list, num); + _lookup_seq_close(cld); +quit1: + _lookup_seq_close(cla); +quit0: + return ret; +} diff --git a/lib/libc/citrus/citrus_esdb.h b/lib/libc/citrus/citrus_esdb.h new file mode 100644 index 0000000000..bf28038780 --- /dev/null +++ b/lib/libc/citrus/citrus_esdb.h @@ -0,0 +1,57 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_esdb.h,v 1.1 2003/06/25 09:51:32 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_esdb.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_ESDB_H_ +#define _CITRUS_ESDB_H_ + +struct _citrus_esdb_charset { + _citrus_csid_t ec_csid; + char *ec_csname; +}; + +struct _citrus_esdb { + char *db_encname; + void *db_variable; + size_t db_len_variable; + int db_num_charsets; + struct _citrus_esdb_charset *db_charsets; + int db_use_invalid; + _citrus_wc_t db_invalid; +}; + +__BEGIN_DECLS +const char *_citrus_esdb_alias(const char *, char *, size_t); +int _citrus_esdb_open(struct _citrus_esdb *, const char *); +void _citrus_esdb_close(struct _citrus_esdb *); +void _citrus_esdb_free_list(char **, size_t); +int _citrus_esdb_get_list(char ***, size_t *); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/citrus_esdb_file.h b/lib/libc/citrus/citrus_esdb_file.h new file mode 100644 index 0000000000..e1593c5ec4 --- /dev/null +++ b/lib/libc/citrus/citrus_esdb_file.h @@ -0,0 +1,46 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_esdb_file.h,v 1.1 2003/06/25 09:51:32 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_esdb_file.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_ESDB_FILE_H_ +#define _CITRUS_ESDB_FILE_H_ + +#define _CITRUS_ESDB_MAGIC "ESDB\0\0\0\0" + +#define _CITRUS_ESDB_SYM_VERSION "version" +#define _CITRUS_ESDB_SYM_ENCODING "encoding" +#define _CITRUS_ESDB_SYM_VARIABLE "variable" +#define _CITRUS_ESDB_SYM_NUM_CHARSETS "num_charsets" +#define _CITRUS_ESDB_SYM_INVALID "invalid" +#define _CITRUS_ESDB_SYM_CSNAME_PREFIX "csname_" +#define _CITRUS_ESDB_SYM_CSID_PREFIX "csid_" + +#define _CITRUS_ESDB_VERSION 0x00000001 + +#endif diff --git a/lib/libc/citrus/citrus_hash.c b/lib/libc/citrus/citrus_hash.c new file mode 100644 index 0000000000..933ce2d183 --- /dev/null +++ b/lib/libc/citrus/citrus_hash.c @@ -0,0 +1,51 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_hash.c,v 1.1 2003/06/25 09:51:32 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_hash.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_region.h" +#include "citrus_hash.h" +#include "citrus_db_hash.h" + +int +_citrus_string_hash_func(const char *key, int hashsize) +{ + struct _region r; + + /* LINTED: discard const */ + _region_init(&r, (char *)key, strlen(key)); + + return (int)(_db_hash_std(NULL, &r) % (uint32_t)hashsize); +} diff --git a/lib/libc/citrus/citrus_hash.h b/lib/libc/citrus/citrus_hash.h new file mode 100644 index 0000000000..8a131f078e --- /dev/null +++ b/lib/libc/citrus/citrus_hash.h @@ -0,0 +1,60 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_hash.h,v 1.3 2004/01/02 21:49:35 itojun Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_hash.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_HASH_H_ +#define _CITRUS_HASH_H_ + +#define _CITRUS_HASH_ENTRY(type) LIST_ENTRY(type) +#define _CITRUS_HASH_HEAD(headname, type, hashsize) \ +struct headname { \ + LIST_HEAD(, type) chh_table[hashsize]; \ +} +#define _CITRUS_HASH_INIT(head, hashsize) \ +do { \ + int _ch_loop; \ + for (_ch_loop = 0; _ch_loop < hashsize; _ch_loop++) \ + LIST_INIT(&(head)->chh_table[_ch_loop]); \ +} while (/*CONSTCOND*/0) +#define _CITRUS_HASH_REMOVE(elm, field) LIST_REMOVE(elm, field) +#define _CITRUS_HASH_INSERT(head, elm, field, hashval) \ +LIST_INSERT_HEAD(&(head)->chh_table[hashval], elm, field) +#define _CITRUS_HASH_SEARCH(head, elm, field, matchfunc, key, hashval) \ +do { \ + LIST_FOREACH((elm), &(head)->chh_table[hashval], field) { \ + if (matchfunc((elm), key)==0) \ + break; \ + } \ +} while (/*CONSTCOND*/0) + +__BEGIN_DECLS +int _citrus_string_hash_func(const char *, int); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/citrus_iconv.c b/lib/libc/citrus/citrus_iconv.c new file mode 100644 index 0000000000..48b27ce824 --- /dev/null +++ b/lib/libc/citrus/citrus_iconv.c @@ -0,0 +1,391 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_iconv.c,v 1.6 2004/12/30 05:03:48 christos Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_iconv.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "un-namespace.h" + +#include "libc_private.h" + +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_module.h" +#include "citrus_lookup.h" +#include "citrus_hash.h" +#include "citrus_iconv.h" + +#define _CITRUS_ICONV_DIR "iconv.dir" +#define _CITRUS_ICONV_ALIAS "iconv.alias" + +#define CI_HASH_SIZE 101 +#define CI_INITIAL_MAX_REUSE 5 +#define CI_ENV_MAX_REUSE "ICONV_MAX_REUSE" + +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +static int isinit = 0; +static _CITRUS_HASH_HEAD(, _citrus_iconv_shared, CI_HASH_SIZE) shared_pool; +static TAILQ_HEAD(, _citrus_iconv_shared) shared_unused; +static int shared_num_unused, shared_max_reuse; + +static __inline void +init_cache(void) +{ + if (__isthreaded) + _pthread_mutex_lock(&lock); + + if (!isinit) { + _CITRUS_HASH_INIT(&shared_pool, CI_HASH_SIZE); + TAILQ_INIT(&shared_unused); + shared_max_reuse = -1; + if (!issetugid() && getenv(CI_ENV_MAX_REUSE)) + shared_max_reuse = atoi(getenv(CI_ENV_MAX_REUSE)); + if (shared_max_reuse < 0) + shared_max_reuse = CI_INITIAL_MAX_REUSE; + isinit = 1; + } + + if (__isthreaded) + _pthread_mutex_unlock(&lock); +} + +/* + * lookup_iconv_entry: + * lookup iconv.dir entry in the specified directory. + * + * line format of iconv.dir file: + * key module arg + * key : lookup key. + * module : iconv module name. + * arg : argument for the module (generally, description file name) + * + */ +static __inline int +lookup_iconv_entry(const char *curdir, const char *key, + char *linebuf, size_t linebufsize, + const char **module, const char **variable) +{ + const char *cp, *cq; + char *p, path[PATH_MAX]; + + /* iconv.dir path */ + snprintf(path, PATH_MAX, "%s/" _CITRUS_ICONV_DIR, curdir); + + /* lookup db */ + cp = p = _lookup_simple(path, key, linebuf, linebufsize, + _LOOKUP_CASE_IGNORE); + if (p == NULL) + return ENOENT; + + /* get module name */ + *module = p; + cq = _bcs_skip_nonws(cp); + p[cq-cp] = '\0'; + p += cq-cp+1; + cq++; + + /* get variable */ + cp = _bcs_skip_ws(cq); + *variable = p += cp - cq; + cq = _bcs_skip_nonws(cp); + p[cq-cp] = '\0'; + + return 0; +} + +static __inline void +close_shared(struct _citrus_iconv_shared *ci) +{ + if (ci) { + if (ci->ci_module) { + if (ci->ci_ops) { + if (ci->ci_closure) + (*ci->ci_ops->io_uninit_shared)(ci); + free(ci->ci_ops); + } + _citrus_unload_module(ci->ci_module); + } + free(ci); + } +} + +static __inline int +open_shared(struct _citrus_iconv_shared * __restrict * __restrict rci, + const char * __restrict basedir, const char * __restrict convname, + const char * __restrict src, const char * __restrict dst) +{ + int ret; + struct _citrus_iconv_shared *ci; + _citrus_iconv_getops_t getops; + char linebuf[LINE_MAX]; + const char *module, *variable; + size_t len_convname; + + /* search converter entry */ + ret = lookup_iconv_entry(basedir, convname, linebuf, sizeof(linebuf), + &module, &variable); + if (ret) { + if (ret == ENOENT) + /* fallback */ + ret = lookup_iconv_entry(basedir, "*", + linebuf, PATH_MAX, + &module, &variable); + if (ret) + return ret; + } + + /* initialize iconv handle */ + len_convname = strlen(convname); + ci = malloc(sizeof(*ci)+len_convname+1); + if (!ci) { + ret = errno; + goto err; + } + ci->ci_module = NULL; + ci->ci_ops = NULL; + ci->ci_closure = NULL; + ci->ci_convname = (void *)&ci[1]; + memcpy(ci->ci_convname, convname, len_convname+1); + + /* load module */ + ret = _citrus_load_module(&ci->ci_module, module); + if (ret) + goto err; + + /* get operators */ + getops = (_citrus_iconv_getops_t) + _citrus_find_getops(ci->ci_module, module, "iconv"); + if (!getops) { + ret = EOPNOTSUPP; + goto err; + } + ci->ci_ops = malloc(sizeof(*ci->ci_ops)); + if (!ci->ci_ops) { + ret = errno; + goto err; + } + ret = (*getops)(ci->ci_ops, sizeof(*ci->ci_ops), + _CITRUS_ICONV_ABI_VERSION); + if (ret) + goto err; + + /* version check */ + if (ci->ci_ops->io_abi_version == 1) { + /* binary compatibility broken at ver.2 */ + ret = EINVAL; + goto err; + } + + if (ci->ci_ops->io_init_shared == NULL || + ci->ci_ops->io_uninit_shared == NULL || + ci->ci_ops->io_init_context == NULL || + ci->ci_ops->io_uninit_context == NULL || + ci->ci_ops->io_convert == NULL) + goto err; + + /* initialize the converter */ + ret = (*ci->ci_ops->io_init_shared)(ci, basedir, src, dst, + (const void *)variable, + strlen(variable)+1); + if (ret) + goto err; + + *rci = ci; + + return 0; +err: + close_shared(ci); + return ret; +} + +static __inline int +hash_func(const char *key) +{ + return _string_hash_func(key, CI_HASH_SIZE); +} + +static __inline int +match_func(struct _citrus_iconv_shared * __restrict ci, + const char * __restrict key) +{ + return strcmp(ci->ci_convname, key); +} + +static int +get_shared(struct _citrus_iconv_shared * __restrict * __restrict rci, + const char *basedir, const char *src, const char *dst) +{ + int ret = 0; + int hashval; + struct _citrus_iconv_shared * ci; + char convname[PATH_MAX]; + + snprintf(convname, sizeof(convname), "%s/%s", src, dst); + + if (__isthreaded) + _pthread_mutex_lock(&lock); + + /* lookup alread existing entry */ + hashval = hash_func(convname); + _CITRUS_HASH_SEARCH(&shared_pool, ci, ci_hash_entry, match_func, + convname, hashval); + if (ci != NULL) { + /* found */ + if (ci->ci_used_count == 0) { + TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry); + shared_num_unused--; + } + ci->ci_used_count++; + *rci = ci; + goto quit; + } + + /* create new entry */ + ret = open_shared(&ci, basedir, convname, src, dst); + if (ret) + goto quit; + + _CITRUS_HASH_INSERT(&shared_pool, ci, ci_hash_entry, hashval); + ci->ci_used_count = 1; + *rci = ci; + +quit: + if (__isthreaded) + _pthread_mutex_unlock(&lock); + + return ret; +} + +static void +release_shared(struct _citrus_iconv_shared * __restrict ci) +{ + if (__isthreaded) + _pthread_mutex_lock(&lock); + + ci->ci_used_count--; + if (ci->ci_used_count == 0) { + /* put it into unused list */ + shared_num_unused++; + TAILQ_INSERT_TAIL(&shared_unused, ci, ci_tailq_entry); + /* flood out */ + while (shared_num_unused > shared_max_reuse) { + ci = TAILQ_FIRST(&shared_unused); + _DIAGASSERT(ci != NULL); + TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry); + _CITRUS_HASH_REMOVE(ci, ci_hash_entry); + shared_num_unused--; + close_shared(ci); + } + } + + if (__isthreaded) + _pthread_mutex_unlock(&lock); +} + +/* + * _citrus_iconv_open: + * open a converter for the specified in/out codes. + */ +int +_citrus_iconv_open(struct _citrus_iconv * __restrict * __restrict rcv, + const char * __restrict basedir, + const char * __restrict src, const char * __restrict dst) +{ + int ret; + struct _citrus_iconv_shared *ci; + struct _citrus_iconv *cv; + char realsrc[PATH_MAX], realdst[PATH_MAX]; + char buf[PATH_MAX], path[PATH_MAX]; + + init_cache(); + + /* resolve codeset name aliases */ + snprintf(path, sizeof(path), "%s/%s", basedir, _CITRUS_ICONV_ALIAS); + strlcpy(realsrc, + _lookup_alias(path, src, buf, PATH_MAX, _LOOKUP_CASE_IGNORE), + PATH_MAX); + strlcpy(realdst, + _lookup_alias(path, dst, buf, PATH_MAX, _LOOKUP_CASE_IGNORE), + PATH_MAX); + + /* sanity check */ + if (strchr(realsrc, '/') != NULL || strchr(realdst, '/')) + return EINVAL; + + /* get shared record */ + ret = get_shared(&ci, basedir, realsrc, realdst); + if (ret) + return ret; + + /* create/init context */ + cv = malloc(sizeof(*cv)); + if (cv == NULL) { + ret = errno; + release_shared(ci); + return ret; + } + cv->cv_shared = ci; + ret = (*ci->ci_ops->io_init_context)(cv); + if (ret) { + release_shared(ci); + free(cv); + return ret; + } + *rcv = cv; + + return 0; +} + +/* + * _citrus_iconv_close: + * close the specified converter. + */ +void +_citrus_iconv_close(struct _citrus_iconv *cv) +{ + if (cv) { + (*cv->cv_shared->ci_ops->io_uninit_context)(cv); + release_shared(cv->cv_shared); + free(cv); + } +} diff --git a/lib/libc/citrus/citrus_iconv.h b/lib/libc/citrus/citrus_iconv.h new file mode 100644 index 0000000000..0d2739a7de --- /dev/null +++ b/lib/libc/citrus/citrus_iconv.h @@ -0,0 +1,69 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_iconv.h,v 1.3 2003/07/10 08:50:43 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_iconv.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_ICONV_H_ +#define _CITRUS_ICONV_H_ + +struct _citrus_iconv_shared; +struct _citrus_iconv_ops; +struct _citrus_iconv; + +__BEGIN_DECLS +int _citrus_iconv_open(struct _citrus_iconv * __restrict * __restrict, + const char * __restrict, + const char * __restrict, const char * __restrict); +void _citrus_iconv_close(struct _citrus_iconv *); +__END_DECLS + + +#include "citrus_iconv_local.h" + +#define _CITRUS_ICONV_F_HIDE_INVALID 0x0001 + +/* + * _citrus_iconv_convert: + * convert a string. + */ +static __inline int +_citrus_iconv_convert(struct _citrus_iconv * __restrict cv, + const char * __restrict * __restrict in, + size_t * __restrict inbytes, + char * __restrict * __restrict out, + size_t * __restrict outbytes, uint32_t flags, + size_t * __restrict nresults) +{ + + _DIAGASSERT(cv && cv->cv_shared && cv->cv_shared->ci_ops && + cv->cv_shared->ci_ops->io_convert && (out || !outbytes)); + + return (*cv->cv_shared->ci_ops->io_convert)(cv, in, inbytes, out, + outbytes, flags, nresults); +} + +#endif diff --git a/lib/libc/citrus/citrus_iconv_local.h b/lib/libc/citrus/citrus_iconv_local.h new file mode 100644 index 0000000000..9c834c9457 --- /dev/null +++ b/lib/libc/citrus/citrus_iconv_local.h @@ -0,0 +1,108 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_iconv_local.h,v 1.2 2003/07/01 09:42:16 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_iconv_local.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_ICONV_LOCAL_H_ +#define _CITRUS_ICONV_LOCAL_H_ + +#define _CITRUS_ICONV_GETOPS_FUNC_BASE(_n_) \ +int _n_(struct _citrus_iconv_ops *, size_t, uint32_t) +#define _CITRUS_ICONV_GETOPS_FUNC(_n_) \ +_CITRUS_ICONV_GETOPS_FUNC_BASE(_citrus_##_n_##_iconv_getops) + +#define _CITRUS_ICONV_DECLS(_m_) \ +static int _citrus_##_m_##_iconv_init_shared \ + (struct _citrus_iconv_shared * __restrict, \ + const char * __restrict, \ + const char * __restrict, const char * __restrict, \ + const void * __restrict, size_t); \ +static void _citrus_##_m_##_iconv_uninit_shared \ + (struct _citrus_iconv_shared *); \ +static int _citrus_##_m_##_iconv_convert \ + (struct _citrus_iconv * __restrict, \ + const char * __restrict * __restrict, size_t * __restrict, \ + char * __restrict * __restrict, size_t * __restrict outbytes, \ + uint32_t, size_t * __restrict); \ +static int _citrus_##_m_##_iconv_init_context \ + (struct _citrus_iconv *); \ +static void _citrus_##_m_##_iconv_uninit_context \ + (struct _citrus_iconv *) + + +#define _CITRUS_ICONV_DEF_OPS(_m_) \ +struct _citrus_iconv_ops _citrus_##_m_##_iconv_ops = { \ + /* io_abi_version */ _CITRUS_ICONV_ABI_VERSION, \ + /* io_init_shared */ &_citrus_##_m_##_iconv_init_shared, \ + /* io_uninit_shared */ &_citrus_##_m_##_iconv_uninit_shared, \ + /* io_init_context */ &_citrus_##_m_##_iconv_init_context, \ + /* io_uninit_context */ &_citrus_##_m_##_iconv_uninit_context, \ + /* io_convert */ &_citrus_##_m_##_iconv_convert \ +} + +typedef _CITRUS_ICONV_GETOPS_FUNC_BASE((*_citrus_iconv_getops_t)); +typedef int (*_citrus_iconv_init_shared_t) + (struct _citrus_iconv_shared * __restrict, + const char * __restrict, const char * __restrict, + const char * __restrict, const void * __restrict, size_t); +typedef void (*_citrus_iconv_uninit_shared_t) + (struct _citrus_iconv_shared *); +typedef int (*_citrus_iconv_convert_t) + (struct _citrus_iconv * __restrict, + const char *__restrict* __restrict, size_t * __restrict, + char * __restrict * __restrict, size_t * __restrict, uint32_t, + size_t * __restrict); +typedef int (*_citrus_iconv_init_context_t)(struct _citrus_iconv *); +typedef void (*_citrus_iconv_uninit_context_t)(struct _citrus_iconv *); + +struct _citrus_iconv_ops { + uint32_t io_abi_version; + _citrus_iconv_init_shared_t io_init_shared; + _citrus_iconv_uninit_shared_t io_uninit_shared; + _citrus_iconv_init_context_t io_init_context; + _citrus_iconv_uninit_context_t io_uninit_context; + _citrus_iconv_convert_t io_convert; +}; +#define _CITRUS_ICONV_ABI_VERSION 2 + +struct _citrus_iconv_shared { + struct _citrus_iconv_ops *ci_ops; + void *ci_closure; + /* private */ + _CITRUS_HASH_ENTRY(_citrus_iconv_shared) ci_hash_entry; + TAILQ_ENTRY(_citrus_iconv_shared) ci_tailq_entry; + _citrus_module_t ci_module; + unsigned int ci_used_count; + char *ci_convname; +}; + +struct _citrus_iconv { + struct _citrus_iconv_shared *cv_shared; + void *cv_closure; +}; + +#endif diff --git a/lib/libc/citrus/citrus_lookup.c b/lib/libc/citrus/citrus_lookup.c new file mode 100644 index 0000000000..6ca99700fa --- /dev/null +++ b/lib/libc/citrus/citrus_lookup.c @@ -0,0 +1,353 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_lookup.c,v 1.3 2004/07/21 14:16:34 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_lookup.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" +#include "citrus_lookup.h" +#include "citrus_lookup_file.h" + +struct _citrus_lookup { + union { + struct { + struct _citrus_db *db; + struct _citrus_region file; + int num, idx; + struct _db_locator locator; + } db; + struct { + struct _region r; + struct _memstream ms; + } plain; + } u; +#define cl_db u.db.db +#define cl_dbidx u.db.idx +#define cl_dbfile u.db.file +#define cl_dbnum u.db.num +#define cl_dblocator u.db.locator +#define cl_plainr u.plain.r +#define cl_plainms u.plain.ms + int cl_ignore_case; + int cl_rewind; + char *cl_key; + size_t cl_keylen; + int (*cl_next)(struct _citrus_lookup *, struct _region *, + struct _region *); + int (*cl_lookup)(struct _citrus_lookup *, const char *, + struct _region *); + int (*cl_num_entries)(struct _citrus_lookup *); + void (*cl_close)(struct _citrus_lookup *); +}; + +static int +seq_get_num_entries_db(struct _citrus_lookup *cl) +{ + return cl->cl_dbnum; +} + +static int +seq_next_db(struct _citrus_lookup *cl, + struct _region *key, struct _region *data) +{ + + if (cl->cl_key) { + if (key) + _region_init(key, cl->cl_key, cl->cl_keylen); + return _db_lookup_by_s(cl->cl_db, cl->cl_key, data, + &cl->cl_dblocator); + } + + if (cl->cl_rewind) { + cl->cl_dbidx = 0; + } + cl->cl_rewind = 0; + if (cl->cl_dbidx >= cl->cl_dbnum) + return ENOENT; + + return _db_get_entry(cl->cl_db, cl->cl_dbidx++, key, data); +} + +static int +seq_lookup_db(struct _citrus_lookup *cl, const char *key, + struct _region *data) +{ + cl->cl_rewind = 0; + free(cl->cl_key); + cl->cl_key = strdup(key); + if (cl->cl_ignore_case) + _bcs_convert_to_lower(cl->cl_key); + cl->cl_keylen = strlen(cl->cl_key); + _db_locator_init(&cl->cl_dblocator); + return _db_lookup_by_s(cl->cl_db, cl->cl_key, data, &cl->cl_dblocator); +} + +static void +seq_close_db(struct _citrus_lookup *cl) +{ + _db_close(cl->cl_db); + _unmap_file(&cl->cl_dbfile); +} + +static int +seq_open_db(struct _citrus_lookup *cl, const char *name) +{ + int ret; + struct _region r; + char path[PATH_MAX]; + + snprintf(path, sizeof(path), "%s.db", name); + ret = _map_file(&r, path); + if (ret) + return ret; + + ret = _db_open(&cl->cl_db, &r, _CITRUS_LOOKUP_MAGIC, + _db_hash_std, NULL); + if (ret) { + _unmap_file(&r); + return ret; + } + + cl->cl_dbfile = r; + cl->cl_dbnum = _db_get_num_entries(cl->cl_db); + cl->cl_dbidx = 0; + cl->cl_rewind = 1; + cl->cl_lookup = &seq_lookup_db; + cl->cl_next = &seq_next_db; + cl->cl_num_entries = &seq_get_num_entries_db; + cl->cl_close = &seq_close_db; + + return 0; +} + +#define T_COMM '#' +static int +seq_next_plain(struct _citrus_lookup *cl, struct _region *key, + struct _region *data) +{ + const char *p, *q; + size_t len; + + if (cl->cl_rewind) + _memstream_bind(&cl->cl_plainms, &cl->cl_plainr); + cl->cl_rewind = 0; + +retry: + p = _memstream_getln(&cl->cl_plainms, &len); + if (p == NULL) + return ENOENT; + /* ignore comment */ + q = memchr(p, T_COMM, len); + if (q) { + len = q-p; + } + /* ignore trailing spaces */ + _bcs_trunc_rws_len(p, &len); + p = _bcs_skip_ws_len(p, &len); + q = _bcs_skip_nonws_len(p, &len); + if (p==q) + goto retry; + if (cl->cl_key && (q-p != cl->cl_keylen || + memcmp(p, cl->cl_key, (size_t)(q-p)) != 0)) + goto retry; + + /* found a entry */ + if (key) + /* LINTED: discard const */ + _region_init(key, (char *)p, q-p); + p = _bcs_skip_ws_len(q, &len); + if (data) + /* LINTED: discard const */ + _region_init(data, len ? (char *)p : NULL, len); + + return 0; +} + +static int +seq_get_num_entries_plain(struct _citrus_lookup *cl) +{ + int num; + + num = 0; + while (seq_next_plain(cl, NULL, NULL) == 0) + num++; + + return num; +} + +static int +seq_lookup_plain(struct _citrus_lookup *cl, const char *key, + struct _region *data) +{ + size_t len; + const char *p; + + cl->cl_rewind = 0; + free(cl->cl_key); + cl->cl_key = strdup(key); + if (cl->cl_ignore_case) + _bcs_convert_to_lower(cl->cl_key); + cl->cl_keylen = strlen(cl->cl_key); + _memstream_bind(&cl->cl_plainms, &cl->cl_plainr); + p = _memstream_matchline(&cl->cl_plainms, cl->cl_key, &len, 0); + if (p == NULL) + return ENOENT; + if (data) + /* LINTED: discard const */ + _region_init(data, (char *)p, len); + + return 0; +} + +static void +seq_close_plain(struct _citrus_lookup *cl) +{ + _unmap_file(&cl->cl_plainr); +} + +static int +seq_open_plain(struct _citrus_lookup *cl, const char *name) +{ + int ret; + + /* open read stream */ + ret = _map_file(&cl->cl_plainr, name); + if (ret) + return ret; + + cl->cl_rewind = 1; + cl->cl_next = &seq_next_plain; + cl->cl_lookup = &seq_lookup_plain; + cl->cl_num_entries = &seq_get_num_entries_plain; + cl->cl_close = &seq_close_plain; + + return 0; +} + +int +_citrus_lookup_seq_open(struct _citrus_lookup **rcl, const char *name, + int ignore_case) +{ + int ret; + struct _citrus_lookup *cl; + + cl = malloc(sizeof(*cl)); + if (cl == NULL) + return errno; + + cl->cl_key = NULL; + cl->cl_keylen = 0; + cl->cl_ignore_case = ignore_case; + ret = seq_open_db(cl, name); + if (ret == ENOENT) + ret = seq_open_plain(cl, name); + if (!ret) + *rcl = cl; + else + free(cl); + + return ret; +} + +void +_citrus_lookup_seq_rewind(struct _citrus_lookup *cl) +{ + cl->cl_rewind = 1; + free(cl->cl_key); + cl->cl_key = NULL; + cl->cl_keylen = 0; +} + +int +_citrus_lookup_seq_next(struct _citrus_lookup *cl, + struct _region *key, struct _region *data) +{ + return (*cl->cl_next)(cl, key, data); +} + +int +_citrus_lookup_seq_lookup(struct _citrus_lookup *cl, const char *key, + struct _region *data) +{ + return (*cl->cl_lookup)(cl, key, data); +} + +int +_citrus_lookup_get_number_of_entries(struct _citrus_lookup *cl) +{ + return (*cl->cl_num_entries)(cl); +} + +void +_citrus_lookup_seq_close(struct _citrus_lookup *cl) +{ + free(cl->cl_key); + (*cl->cl_close)(cl); +} + +char * +_citrus_lookup_simple(const char *name, const char *key, + char *linebuf, size_t linebufsize, int ignore_case) +{ + int ret; + struct _citrus_lookup *cl; + struct _region data; + + ret = _citrus_lookup_seq_open(&cl, name, ignore_case); + if (ret) + return NULL; + + ret = _citrus_lookup_seq_lookup(cl, key, &data); + if (ret) { + _citrus_lookup_seq_close(cl); + return NULL; + } + + snprintf(linebuf, linebufsize, "%.*s", + (int)_region_size(&data), (const char *)_region_head(&data)); + + _citrus_lookup_seq_close(cl); + + return linebuf; +} diff --git a/lib/libc/citrus/citrus_lookup.h b/lib/libc/citrus/citrus_lookup.h new file mode 100644 index 0000000000..16562e8039 --- /dev/null +++ b/lib/libc/citrus/citrus_lookup.h @@ -0,0 +1,62 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_lookup.h,v 1.2 2004/07/21 14:16:34 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_lookup.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_LOOKUP_H_ +#define _CITRUS_LOOKUP_H_ + +#define _CITRUS_LOOKUP_CASE_SENSITIVE 0 +#define _CITRUS_LOOKUP_CASE_IGNORE 1 + +struct _citrus_lookup; +__BEGIN_DECLS +char *_citrus_lookup_simple(const char *, const char *, char *, size_t, int); +int _citrus_lookup_seq_open(struct _citrus_lookup **, const char *, int); +void _citrus_lookup_seq_rewind(struct _citrus_lookup *); +int _citrus_lookup_seq_next(struct _citrus_lookup *, + struct _region *, struct _region *); +int _citrus_lookup_seq_lookup(struct _citrus_lookup *, const char *, + struct _region *); +int _citrus_lookup_get_number_of_entries(struct _citrus_lookup *); +void _citrus_lookup_seq_close(struct _citrus_lookup *); +__END_DECLS + +static __inline const char * +_citrus_lookup_alias(const char *path, const char *key, char *buf, size_t n, + int ignore_case) +{ + const char *ret; + + ret = _citrus_lookup_simple(path, key, buf, n, ignore_case); + if (ret == NULL) + ret = key; + + return ret; +} + +#endif diff --git a/lib/libc/citrus/citrus_lookup_factory.c b/lib/libc/citrus/citrus_lookup_factory.c new file mode 100644 index 0000000000..8b9b46037e --- /dev/null +++ b/lib/libc/citrus/citrus_lookup_factory.c @@ -0,0 +1,120 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_lookup_factory.c,v 1.4 2003/10/27 00:12:42 lukem Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_lookup_factory.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_region.h" +#include "citrus_bcs.h" +#include "citrus_db_factory.h" +#include "citrus_db_hash.h" +#include "citrus_lookup_factory.h" +#include "citrus_lookup_file.h" + +#define T_COMM '#' +static int +convert_line(struct _citrus_db_factory *df, const char *line, size_t len) +{ + const char *p; + char key[LINE_MAX], data[LINE_MAX]; + + /* cut off trailing comment */ + p = memchr(line, T_COMM, len); + if (p) + len = p - line; + + /* key */ + line = _bcs_skip_ws_len(line, &len); + if (len == 0) + return 0; + p = _bcs_skip_nonws_len(line, &len); + if (p==line) + return 0; + snprintf(key, sizeof(key), "%.*s", (int)(p-line), line); + _bcs_convert_to_lower(key); + + /* data */ + line = _bcs_skip_ws_len(p, &len); + _bcs_trunc_rws_len(line, &len); + snprintf(data, sizeof(data), "%.*s", (int)len, line); + + return _db_factory_addstr_by_s(df, key, data); +} + +static int +dump_db(struct _citrus_db_factory *df, struct _region *r) +{ + size_t size; + void *ptr; + + size = _db_factory_calc_size(df); + ptr = malloc(size); + if (ptr == NULL) + return errno; + _region_init(r, ptr, size); + + return _db_factory_serialize(df, _CITRUS_LOOKUP_MAGIC, r); +} + +int +_citrus_lookup_factory_convert(FILE *out, FILE *in) +{ + struct _citrus_db_factory *df; + struct _region r; + char *line; + size_t size; + int ret; + + ret = _db_factory_create(&df, &_db_hash_std, NULL); + if (ret) + return ret; + + while ((line = fgetln(in, &size)) != NULL) + if ((ret = convert_line(df, line, size))) { + _db_factory_free(df); + return ret; + } + + ret = dump_db(df, &r); + _db_factory_free(df); + if (ret) + return ret; + + if (fwrite(_region_head(&r), _region_size(&r), 1, out) != 1) + return errno; + + return 0; +} diff --git a/lib/libc/citrus/citrus_lookup_factory.h b/lib/libc/citrus/citrus_lookup_factory.h new file mode 100644 index 0000000000..fa20d4d365 --- /dev/null +++ b/lib/libc/citrus/citrus_lookup_factory.h @@ -0,0 +1,37 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_lookup_factory.h,v 1.1 2003/06/25 09:51:35 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_lookup_factory.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_LOOKUP_FACTORY_H_ +#define _CITRUS_LOOKUP_FACTORY_H_ + +__BEGIN_DECLS +int _citrus_lookup_factory_convert(FILE *, FILE *); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/citrus_lookup_file.h b/lib/libc/citrus/citrus_lookup_file.h new file mode 100644 index 0000000000..728822c4c4 --- /dev/null +++ b/lib/libc/citrus/citrus_lookup_file.h @@ -0,0 +1,35 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_lookup_factory.h,v 1.1 2003/06/25 09:51:35 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_lookup_file.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_LOOKUP_FILE_H_ +#define _CITRUS_LOOKUP_FILE_H_ + +#define _CITRUS_LOOKUP_MAGIC "LOOKUP\0\0" + +#endif diff --git a/lib/libc/citrus/citrus_mapper.c b/lib/libc/citrus/citrus_mapper.c new file mode 100644 index 0000000000..0392ca6236 --- /dev/null +++ b/lib/libc/citrus/citrus_mapper.c @@ -0,0 +1,412 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_mapper.c,v 1.6 2004/12/30 05:05:01 christos Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_mapper.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "namespace.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "un-namespace.h" + +#include "libc_private.h" + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_bcs.h" +#include "citrus_mmap.h" +#include "citrus_module.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" + +#define _CITRUS_MAPPER_DIR "mapper.dir" + +#define CM_HASH_SIZE 101 +#define REFCOUNT_PERSISTENT -1 + +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; + +struct _citrus_mapper_area { + _CITRUS_HASH_HEAD(, _citrus_mapper, CM_HASH_SIZE) ma_cache; + char *ma_dir; +}; + +/* + * _citrus_mapper_create_area: + * create mapper area + */ + +int +_citrus_mapper_create_area( + struct _citrus_mapper_area *__restrict *__restrict rma, + const char *__restrict area) +{ + struct stat st; + int ret; + char path[PATH_MAX]; + struct _citrus_mapper_area *ma; + + if (__isthreaded) + _pthread_mutex_lock(&lock); + + if (*rma != NULL) { + ret = 0; + goto quit; + } + + snprintf(path, PATH_MAX, "%s/%s", area, _CITRUS_MAPPER_DIR); + + ret = stat(path, &st); + if (ret) + goto quit; + + ma = malloc(sizeof(*ma)); + if (ma == NULL) { + ret = errno; + goto quit; + } + ma->ma_dir = strdup(area); + if (ma->ma_dir == NULL) { + ret = errno; + free(ma->ma_dir); + goto quit; + } + _CITRUS_HASH_INIT(&ma->ma_cache, CM_HASH_SIZE); + + *rma = ma; + ret = 0; +quit: + if (__isthreaded) + _pthread_mutex_unlock(&lock); + + return ret; +} + + +/* + * lookup_mapper_entry: + * lookup mapper.dir entry in the specified directory. + * + * line format of iconv.dir file: + * mapper module arg + * mapper : mapper name. + * module : mapper module name. + * arg : argument for the module (generally, description file name) + */ + +static int +lookup_mapper_entry(const char *dir, const char *mapname, + void *linebuf, size_t linebufsize, + const char **module, const char **variable) +{ + struct _region r; + struct _memstream ms; + int ret; + const char *cp, *cq; + char *p; + size_t len; + char path[PATH_MAX]; + + /* create mapper.dir path */ + snprintf(path, PATH_MAX, "%s/%s", dir, _CITRUS_MAPPER_DIR); + + /* open read stream */ + ret = _map_file(&r, path); + if (ret) + return ret; + + _memstream_bind(&ms, &r); + + /* search the line matching to the map name */ + cp = _memstream_matchline(&ms, mapname, &len, 0); + if (!cp) { + ret = ENOENT; + goto quit; + } + if (!len || len>linebufsize-1) { + ret = EINVAL; + goto quit; + } + + p = linebuf; + /* get module name */ + *module = p; + cq = _bcs_skip_nonws_len(cp, &len); + strlcpy(p, cp, (size_t)(cq-cp+1)); + p += cq-cp+1; + + /* get variable */ + *variable = p; + cp = _bcs_skip_ws_len(cq, &len); + strlcpy(p, cp, len+1); + + ret = 0; + +quit: + _unmap_file(&r); + return ret; +} + +/* + * mapper_close: + * simply close a mapper. (without handling hash) + */ +static void +mapper_close(struct _citrus_mapper *cm) +{ + if (cm->cm_module) { + if (cm->cm_ops) { + if (cm->cm_closure) + (*cm->cm_ops->mo_uninit)(cm); + free(cm->cm_ops); + } + _citrus_unload_module(cm->cm_module); + } + free(cm->cm_traits); + free(cm); +} + +/* + * mapper_open: + * simply open a mapper. (without handling hash) + */ +static int +mapper_open(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict * __restrict rcm, + const char * __restrict module, + const char * __restrict variable) +{ + int ret; + struct _citrus_mapper *cm; + _citrus_mapper_getops_t getops; + + /* initialize mapper handle */ + cm = malloc(sizeof(*cm)); + if (!cm) + return errno; + + cm->cm_module = NULL; + cm->cm_ops = NULL; + cm->cm_closure = NULL; + cm->cm_traits = NULL; + cm->cm_refcount = 0; + cm->cm_key = NULL; + + /* load module */ + ret = _citrus_load_module(&cm->cm_module, module); + if (ret) + goto err; + + /* get operators */ + getops = (_citrus_mapper_getops_t) + _citrus_find_getops(cm->cm_module, module, "mapper"); + if (!getops) { + ret = EOPNOTSUPP; + goto err; + } + cm->cm_ops = malloc(sizeof(*cm->cm_ops)); + if (!cm->cm_ops) { + ret = errno; + goto err; + } + ret = (*getops)(cm->cm_ops, sizeof(*cm->cm_ops), + _CITRUS_MAPPER_ABI_VERSION); + if (ret) + goto err; + + if (!cm->cm_ops->mo_init || + !cm->cm_ops->mo_uninit || + !cm->cm_ops->mo_convert || + !cm->cm_ops->mo_init_state) + goto err; + + /* allocate traits structure */ + cm->cm_traits = malloc(sizeof(*cm->cm_traits)); + if (cm->cm_traits == NULL) { + ret = errno; + goto err; + } + /* initialize the mapper */ + ret = (*cm->cm_ops->mo_init)(ma, cm, ma->ma_dir, + (const void *)variable, + strlen(variable)+1, + cm->cm_traits, sizeof(*cm->cm_traits)); + if (ret) + goto err; + + *rcm = cm; + + return 0; + +err: + mapper_close(cm); + return ret; +} + +/* + * _citrus_mapper_open_direct: + * open a mapper. + */ +int +_citrus_mapper_open_direct(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict * __restrict rcm, + const char * __restrict module, + const char * __restrict variable) +{ + return mapper_open(ma, rcm, module, variable); +} + +/* + * hash_func + */ +static __inline int +hash_func(const char *key) +{ + return _string_hash_func(key, CM_HASH_SIZE); +} + +/* + * match_func + */ +static __inline int +match_func(struct _citrus_mapper *cm, const char *key) +{ + return strcmp(cm->cm_key, key); +} + +/* + * _citrus_mapper_open: + * open a mapper with looking up "mapper.dir". + */ +int +_citrus_mapper_open(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict * __restrict rcm, + const char * __restrict mapname) +{ + int ret; + char linebuf[PATH_MAX]; + const char *module, *variable; + struct _citrus_mapper *cm; + int hashval; + + if (__isthreaded) + _pthread_mutex_lock(&lock); + + /* search in the cache */ + hashval = hash_func(mapname); + _CITRUS_HASH_SEARCH(&ma->ma_cache, cm, cm_entry, match_func, mapname, + hashval); + if (cm) { + /* found */ + cm->cm_refcount++; + *rcm = cm; + ret = 0; + goto quit; + } + + /* search mapper entry */ + ret = lookup_mapper_entry(ma->ma_dir, mapname, linebuf, PATH_MAX, + &module, &variable); + if (ret) + goto quit; + + /* open mapper */ + ret = mapper_open(ma, &cm, module, variable); + if (ret) + goto quit; + cm->cm_key = strdup(mapname); + if (cm->cm_key == NULL) { + ret = errno; + if (__isthreaded) + _pthread_mutex_unlock(&lock); + _mapper_close(cm); + return ret; + } + + /* insert to the cache */ + cm->cm_refcount = 1; + _CITRUS_HASH_INSERT(&ma->ma_cache, cm, cm_entry, hashval); + + *rcm = cm; + ret = 0; +quit: + if (__isthreaded) + _pthread_mutex_unlock(&lock); + return ret; +} + +/* + * _citrus_mapper_close: + * close the specified mapper. + */ +void +_citrus_mapper_close(struct _citrus_mapper *cm) +{ + if (cm) { + if (__isthreaded) + _pthread_mutex_lock(&lock); + if (cm->cm_refcount == REFCOUNT_PERSISTENT) + goto quit; + if (cm->cm_refcount > 0) { + if (--cm->cm_refcount > 0) + goto quit; + _CITRUS_HASH_REMOVE(cm, cm_entry); + free(cm->cm_key); + } + if (__isthreaded) + _pthread_mutex_unlock(&lock); + mapper_close(cm); + return; +quit: + if (__isthreaded) + _pthread_mutex_unlock(&lock); + } +} + +/* + * _citrus_mapper_set_persistent: + * set persistent count. + */ +void +_citrus_mapper_set_persistent(struct _citrus_mapper * __restrict cm) +{ + if (__isthreaded) + _pthread_mutex_lock(&lock); + cm->cm_refcount = REFCOUNT_PERSISTENT; + if (__isthreaded) + _pthread_mutex_unlock(&lock); +} diff --git a/lib/libc/citrus/citrus_mapper.h b/lib/libc/citrus/citrus_mapper.h new file mode 100644 index 0000000000..c466b56c70 --- /dev/null +++ b/lib/libc/citrus/citrus_mapper.h @@ -0,0 +1,143 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_mapper.h,v 1.3 2003/07/12 15:39:19 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_mapper.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_MAPPER_H_ +#define _CITRUS_MAPPER_H_ + +struct _citrus_mapper_area; +struct _citrus_mapper; +struct _citrus_mapper_ops; +struct _citrus_mapper_traits; + +__BEGIN_DECLS +int _citrus_mapper_create_area( + struct _citrus_mapper_area *__restrict *__restrict, + const char *__restrict); +int _citrus_mapper_open(struct _citrus_mapper_area *__restrict, + struct _citrus_mapper *__restrict *__restrict, + const char *__restrict); +int _citrus_mapper_open_direct( + struct _citrus_mapper_area *__restrict, + struct _citrus_mapper *__restrict *__restrict, + const char *__restrict, const char *__restrict); +void _citrus_mapper_close(struct _citrus_mapper *); +void _citrus_mapper_set_persistent(struct _citrus_mapper * __restrict); +__END_DECLS + +#include "citrus_mapper_local.h" + +/* return values of _citrus_mapper_convert */ +#define _CITRUS_MAPPER_CONVERT_SUCCESS (0) +#define _CITRUS_MAPPER_CONVERT_NONIDENTICAL (1) +#define _CITRUS_MAPPER_CONVERT_SRC_MORE (2) +#define _CITRUS_MAPPER_CONVERT_DST_MORE (3) +#define _CITRUS_MAPPER_CONVERT_ILSEQ (4) +#define _CITRUS_MAPPER_CONVERT_FATAL (5) + +/* + * _citrus_mapper_convert: + * convert an index. + * - if the converter supports M:1 converter, the function may return + * _CITRUS_MAPPER_CONVERT_SRC_MORE and the storage pointed by dst + * may be unchanged in this case, although the internal status of + * the mapper is affected. + * - if the converter supports 1:N converter, the function may return + * _CITRUS_MAPPER_CONVERT_DST_MORE. In this case, the contiguous + * call of this function ignores src and changes the storage pointed + * by dst. + * - if the converter supports M:N converter, the function may behave + * the combination of the above. + * + */ +static __inline int +_citrus_mapper_convert(struct _citrus_mapper * __restrict cm, + _citrus_index_t * __restrict dst, + _citrus_index_t src, + void * __restrict ps) +{ + + _DIAGASSERT(cm && cm->cm_ops && cm->cm_ops->mo_convert && dst); + + return (*cm->cm_ops->mo_convert)(cm, dst, src, ps); +} + +/* + * _citrus_mapper_init_state: + * initialize the state. + */ +static __inline void +_citrus_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ + + _DIAGASSERT(cm && cm->cm_ops && cm->cm_ops->mo_init_state); + + (*cm->cm_ops->mo_init_state)(cm, ps); +} + +/* + * _citrus_mapper_get_state_size: + * get the size of state storage. + */ +static __inline size_t +_citrus_mapper_get_state_size(struct _citrus_mapper * __restrict cm) +{ + + _DIAGASSERT(cm && cm->cm_traits); + + return cm->cm_traits->mt_state_size; +} + +/* + * _citrus_mapper_get_src_max: + * get the maximum number of suspended sources. + */ +static __inline size_t +_citrus_mapper_get_src_max(struct _citrus_mapper * __restrict cm) +{ + + _DIAGASSERT(cm && cm->cm_traits); + + return cm->cm_traits->mt_src_max; +} + +/* + * _citrus_mapper_get_dst_max: + * get the maximum number of suspended destinations. + */ +static __inline size_t +_citrus_mapper_get_dst_max(struct _citrus_mapper * __restrict cm) +{ + + _DIAGASSERT(cm && cm->cm_traits); + + return cm->cm_traits->mt_dst_max; +} + +#endif diff --git a/lib/libc/citrus/citrus_mapper_local.h b/lib/libc/citrus/citrus_mapper_local.h new file mode 100644 index 0000000000..ddef553e45 --- /dev/null +++ b/lib/libc/citrus/citrus_mapper_local.h @@ -0,0 +1,106 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_mapper_local.h,v 1.1 2003/06/25 09:51:36 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_mapper_local.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_MAPPER_LOCAL_H_ +#define _CITRUS_MAPPER_LOCAL_H_ + +#define _CITRUS_MAPPER_GETOPS_FUNC_BASE(_n_) \ +int _n_(struct _citrus_mapper_ops *, size_t, u_int32_t) +#define _CITRUS_MAPPER_GETOPS_FUNC(_n_) \ +_CITRUS_MAPPER_GETOPS_FUNC_BASE(_citrus_##_n_##_mapper_getops) + +#define _CITRUS_MAPPER_DECLS(_m_) \ +static int _citrus_##_m_##_mapper_init \ + (struct _citrus_mapper_area *__restrict, \ + struct _citrus_mapper * __restrict, const char * __restrict, \ + const void * __restrict, size_t, \ + struct _citrus_mapper_traits * __restrict, size_t); \ +static void _citrus_##_m_##_mapper_uninit(struct _citrus_mapper *); \ +static int _citrus_##_m_##_mapper_convert \ + (struct _citrus_mapper * __restrict, \ + _citrus_index_t * __restrict, _citrus_index_t, \ + void * __restrict); \ +static void _citrus_##_m_##_mapper_init_state \ + (struct _citrus_mapper * __restrict, void * __restrict); + +#define _CITRUS_MAPPER_DEF_OPS(_m_) \ +struct _citrus_mapper_ops _citrus_##_m_##_mapper_ops = { \ + /* mo_abi_version */ _CITRUS_MAPPER_ABI_VERSION, \ + /* mo_init */ &_citrus_##_m_##_mapper_init, \ + /* mo_uninit */ &_citrus_##_m_##_mapper_uninit, \ + /* mo_convert */ &_citrus_##_m_##_mapper_convert, \ + /* mo_init_state */ &_citrus_##_m_##_mapper_init_state \ +} + +typedef _CITRUS_MAPPER_GETOPS_FUNC_BASE((*_citrus_mapper_getops_t)); +typedef int (*_citrus_mapper_init_t)( + struct _citrus_mapper_area *__restrict, + struct _citrus_mapper *__restrict, const char *__restrict, + const void *__restrict, size_t, + struct _citrus_mapper_traits * __restrict, size_t); +typedef void (*_citrus_mapper_uninit_t)(struct _citrus_mapper *); +typedef int (*_citrus_mapper_convert_t)(struct _citrus_mapper * __restrict, + _citrus_index_t * __restrict, + _citrus_index_t, + void * __restrict); +typedef void (*_citrus_mapper_init_state_t)( + struct _citrus_mapper * __restrict, void * __restrict); + +/* + * ABI version change log + * 0x00000001 + * initial version + */ +#define _CITRUS_MAPPER_ABI_VERSION 0x00000001 +struct _citrus_mapper_ops { + uint32_t mo_abi_version; + /* version 0x00000001 */ + _citrus_mapper_init_t mo_init; + _citrus_mapper_uninit_t mo_uninit; + _citrus_mapper_convert_t mo_convert; + _citrus_mapper_init_state_t mo_init_state; +}; + +struct _citrus_mapper_traits { + /* version 0x00000001 */ + size_t mt_state_size; + size_t mt_src_max; + size_t mt_dst_max; +}; + +struct _citrus_mapper { + struct _citrus_mapper_ops *cm_ops; + void *cm_closure; + _citrus_module_t cm_module; + struct _citrus_mapper_traits *cm_traits; + _CITRUS_HASH_ENTRY(_citrus_mapper) cm_entry; + int cm_refcount; + char *cm_key; +}; +#endif diff --git a/lib/libc/citrus/citrus_memstream.c b/lib/libc/citrus/citrus_memstream.c new file mode 100644 index 0000000000..df8ff722ce --- /dev/null +++ b/lib/libc/citrus/citrus_memstream.c @@ -0,0 +1,149 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_memstream.c,v 1.2 2004/01/02 21:49:35 itojun Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_memstream.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_bcs.h" + +const char * +_citrus_memory_stream_getln(struct _citrus_memory_stream * __restrict ms, + size_t * __restrict rlen) +{ + int i; + const uint8_t *h, *p; + size_t ret; + + if (ms->ms_pos>=_region_size(&ms->ms_region)) + return (NULL); + + h = p = (uint8_t *)_region_offset(&ms->ms_region, ms->ms_pos); + ret = 0; + for (i = _region_size(&ms->ms_region) - ms->ms_pos; i > 0; i--) { + ret++; + if (_bcs_iseol(*p)) + break; + p++; + } + + ms->ms_pos += ret; + *rlen = ret; + return ((const char *)h); +} + +#define T_COMM '#' + +const char * +_citrus_memory_stream_matchline(struct _citrus_memory_stream * __restrict ms, + const char * __restrict key, + size_t * __restrict rlen, + int iscasesensitive) +{ + const char *p, *q; + size_t len, keylen; + + keylen = strlen(key); + while (/*CONSTCOND*/ 1) { + p = _citrus_memory_stream_getln(ms, &len); + if (p == NULL) + return (NULL); + + /* ignore comment */ + q = memchr(p, T_COMM, len); + if (q) { + len = q-p; + } + /* ignore trailing white space and newline */ + _bcs_trunc_rws_len(p, &len); + if (len == 0) + continue; /* ignore null line */ + + /* skip white spaces at the head of the line */ + p = _bcs_skip_ws_len(p, &len); + q = _bcs_skip_nonws_len(p, &len); + + if (q-p == keylen) { + if (iscasesensitive) { + if (memcmp(key, p, keylen) == 0) + break; /* match */ + } else { + if (_bcs_strncasecmp(key, p, keylen) == 0) + break; /* match */ + } + } + } + + p = _bcs_skip_ws_len(q, &len); + *rlen = len; + + return (p); +} + +void * +_citrus_memory_stream_chr(struct _citrus_memory_stream *ms, + struct _citrus_region *r, char ch) +{ + void *head, *chr; + size_t sz; + + if (ms->ms_pos >= _region_size(&ms->ms_region)) + return NULL; + + head = _region_offset(&ms->ms_region, ms->ms_pos); + chr = memchr(head, ch, _memstream_remainder(ms)); + if (chr == NULL) { + _region_init(r, head, _memstream_remainder(ms)); + ms->ms_pos = _region_size(&ms->ms_region); + return NULL; + } + sz = (char *)chr - (char *)head; + + _region_init(r, head, sz); + ms->ms_pos += sz+1; + + return chr; +} + +void +_citrus_memory_stream_skip_ws(struct _citrus_memory_stream *ms) +{ + int ch; + + while ((ch = _memstream_peek(ms)) != EOF) { + if (!_bcs_isspace(ch)) + break; + _memstream_getc(ms); + } +} diff --git a/lib/libc/citrus/citrus_memstream.h b/lib/libc/citrus/citrus_memstream.h new file mode 100644 index 0000000000..ac4c9e7825 --- /dev/null +++ b/lib/libc/citrus/citrus_memstream.h @@ -0,0 +1,176 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_memstream.h,v 1.2 2004/12/30 05:01:19 christos Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_memstream.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#ifndef _CITRUS_MEMSTREAM_H_ +#define _CITRUS_MEMSTREAM_H_ + +struct _citrus_memory_stream { + struct _citrus_region ms_region; + size_t ms_pos; +}; + +__BEGIN_DECLS +const char * _citrus_memory_stream_getln( + struct _citrus_memory_stream * __restrict, size_t * __restrict); +const char * _citrus_memory_stream_matchline( + struct _citrus_memory_stream * __restrict, const char * __restrict, + size_t * __restrict, int); +void * _citrus_memory_stream_chr(struct _citrus_memory_stream *, + struct _citrus_region *, char); +void _citrus_memory_stream_skip_ws(struct _citrus_memory_stream *); +__END_DECLS + +static __inline int +_citrus_memory_stream_iseof(struct _citrus_memory_stream *ms) +{ + return ms->ms_pos >= _citrus_region_size(&ms->ms_region); +} + +static __inline void +_citrus_memory_stream_bind(struct _citrus_memory_stream * __restrict ms, + const struct _citrus_region * __restrict r) +{ + ms->ms_region = *r; + ms->ms_pos = 0; +} + +static __inline void +_citrus_memory_stream_rewind(struct _citrus_memory_stream *ms) +{ + ms->ms_pos = 0; +} + +static __inline size_t +_citrus_memory_stream_remainder(struct _citrus_memory_stream *ms) +{ + size_t sz; + sz = _citrus_region_size(&ms->ms_region); + if (ms->ms_pos>sz) + return 0; + return sz-ms->ms_pos; +} + +static __inline int +_citrus_memory_stream_seek(struct _citrus_memory_stream *ms, size_t pos, int w) +{ + size_t sz = _citrus_region_size(&ms->ms_region); + switch (w) { + case SEEK_SET: + if (pos>=sz) + return -1; + ms->ms_pos = pos; + break; + case SEEK_CUR: + pos += ms->ms_pos; + if (pos>=sz) + return -1; + break; + case SEEK_END: + if (szms_pos = sz - pos; + break; + } + return 0; +} + +static __inline int +_citrus_memory_stream_getc(struct _citrus_memory_stream *ms) +{ + if (_citrus_memory_stream_iseof(ms)) + return (EOF); + return _citrus_region_peek8(&ms->ms_region, ms->ms_pos++); +} + +static __inline int +_citrus_memory_stream_peek(struct _citrus_memory_stream *ms) +{ + if (_citrus_memory_stream_iseof(ms)) + return (EOF); + return _citrus_region_peek8(&ms->ms_region, ms->ms_pos); +} + +static __inline void * +_citrus_memory_stream_getregion(struct _citrus_memory_stream *ms, + struct _citrus_region *r, size_t sz) +{ + void *ret; + + if (ms->ms_pos + sz > _citrus_region_size(&ms->ms_region)) + return NULL; + + ret = _citrus_region_offset(&ms->ms_region, ms->ms_pos); + ms->ms_pos += sz; + if (r) + _citrus_region_init(r, ret, sz); + + return ret; +} + +static __inline int +_citrus_memory_stream_get8(struct _citrus_memory_stream *ms, uint8_t *rval) +{ + + if (ms->ms_pos + 1 > _citrus_region_size(&ms->ms_region)) + return -1; + + *rval = _citrus_region_peek8(&ms->ms_region, ms->ms_pos); + ms->ms_pos += 2; + + return 0; +} + +static __inline int +_citrus_memory_stream_get16(struct _citrus_memory_stream *ms, uint16_t *rval) +{ + + if (ms->ms_pos + 2 > _citrus_region_size(&ms->ms_region)) + return -1; + + *rval = _citrus_region_peek16(&ms->ms_region, ms->ms_pos); + ms->ms_pos += 2; + + return 0; +} + +static __inline int +_citrus_memory_stream_get32(struct _citrus_memory_stream *ms, uint32_t *rval) +{ + + if (ms->ms_pos + 4 > _citrus_region_size(&ms->ms_region)) + return -1; + + *rval = _citrus_region_peek32(&ms->ms_region, ms->ms_pos); + ms->ms_pos += 4; + + return 0; +} + +#endif diff --git a/lib/libc/citrus/citrus_mmap.c b/lib/libc/citrus/citrus_mmap.c new file mode 100644 index 0000000000..67fc73899e --- /dev/null +++ b/lib/libc/citrus/citrus_mmap.c @@ -0,0 +1,96 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_mmap.c,v 1.3 2005/01/19 00:52:37 mycroft Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_mmap.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_region.h" +#include "citrus_mmap.h" + +int +_citrus_map_file(struct _citrus_region * __restrict r, + const char * __restrict path) +{ + int fd, ret = 0; + struct stat st; + void *head; + + _DIAGASSERT(r != NULL); + + _region_init(r, NULL, 0); + + if ((fd = open(path, O_RDONLY)) == -1) + return errno; + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { + ret = errno; + goto error; + } + + if (fstat(fd, &st) == -1) { + ret = errno; + goto error; + } + if (!S_ISREG(st.st_mode)) { + ret = EOPNOTSUPP; + goto error; + } + + head = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE, + fd, (off_t)0); + if (head == MAP_FAILED) { + ret = errno; + goto error; + } + _region_init(r, head, (size_t)st.st_size); + +error: + (void)close(fd); + return ret; +} + +void +_citrus_unmap_file(struct _citrus_region *r) +{ + _DIAGASSERT(r != NULL); + + if (_region_head(r) != NULL) { + (void)munmap(_region_head(r), _region_size(r)); + _region_init(r, NULL, 0); + } +} diff --git a/lib/libc/citrus/citrus_mmap.h b/lib/libc/citrus/citrus_mmap.h new file mode 100644 index 0000000000..071d34d579 --- /dev/null +++ b/lib/libc/citrus/citrus_mmap.h @@ -0,0 +1,40 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_mmap.h,v 1.1 2003/06/25 09:51:38 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_mmap.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#ifndef _CITRUS_MMAP_H_ +#define _CITRUS_MMAP_H_ + +__BEGIN_DECLS +int _citrus_map_file(struct _citrus_region * __restrict, + const char * __restrict); +void _citrus_unmap_file(struct _citrus_region *); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/citrus_module.c b/lib/libc/citrus/citrus_module.c new file mode 100644 index 0000000000..499fa7de82 --- /dev/null +++ b/lib/libc/citrus/citrus_module.c @@ -0,0 +1,358 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_module.c,v 1.4 2004/12/21 09:00:01 yamt Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_module.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)1999, 2000, 2001, 2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Paul Kranenburg. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "citrus_module.h" + +#include +#include +#include + +#ifdef _I18N_DYNAMIC + +static int _getdewey(int [], char *); +static int _cmpndewey(int [], int, int [], int); +static const char *_findshlib(char *, int *, int *); + +static char *_pathI18nModule = NULL; + +/* from libexec/ld.aout_so/shlib.c */ +#undef major +#undef minor +#define MAXDEWEY 3 /*ELF*/ + +static int +_getdewey(int dewey[], char *cp) +{ + int i, n; + + _DIAGASSERT(dewey != NULL); + _DIAGASSERT(cp != NULL); + + for (n = 0, i = 0; i < MAXDEWEY; i++) { + if (*cp == '\0') + break; + + if (*cp == '.') cp++; + if (*cp < '0' || '9' < *cp) + return 0; + + dewey[n++] = (int)strtol(cp, &cp, 10); + } + + return n; +} + +/* + * Compare two dewey arrays. + * Return -1 if `d1' represents a smaller value than `d2'. + * Return 1 if `d1' represents a greater value than `d2'. + * Return 0 if equal. + */ +static int +_cmpndewey(int d1[], int n1, int d2[], int n2) +{ + register int i; + + _DIAGASSERT(d1 != NULL); + _DIAGASSERT(d2 != NULL); + + for (i = 0; i < n1 && i < n2; i++) { + if (d1[i] < d2[i]) + return -1; + if (d1[i] > d2[i]) + return 1; + } + + if (n1 == n2) + return 0; + + if (i == n1) + return -1; + + if (i == n2) + return 1; + + /* XXX cannot happen */ + return 0; +} + +static const char * +_findshlib(char *name, int *majorp, int *minorp) +{ + int dewey[MAXDEWEY]; + int ndewey; + int tmp[MAXDEWEY]; + int i; + int len; + char *lname; + static char path[PATH_MAX]; + int major, minor; + const char *search_dirs[1]; + const int n_search_dirs = 1; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(majorp != NULL); + _DIAGASSERT(minorp != NULL); + + major = *majorp; + minor = *minorp; + path[0] = '\0'; + search_dirs[0] = _pathI18nModule; + len = strlen(name); + lname = name; + + ndewey = 0; + + for (i = 0; i < n_search_dirs; i++) { + DIR *dd = opendir(search_dirs[i]); + struct dirent *dp; + int found_dot_a = 0; + int found_dot_so = 0; + + if (dd == NULL) + continue; + + while ((dp = readdir(dd)) != NULL) { + int n; + + if (dp->d_namlen < len + 4) + continue; + if (strncmp(dp->d_name, lname, (size_t)len) != 0) + continue; + if (strncmp(dp->d_name+len, ".so.", 4) != 0) + continue; + + if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0) + continue; + + if (major != -1 && found_dot_a) + found_dot_a = 0; + + /* XXX should verify the library is a.out/ELF? */ + + if (major == -1 && minor == -1) { + goto compare_version; + } else if (major != -1 && minor == -1) { + if (tmp[0] == major) + goto compare_version; + } else if (major != -1 && minor != -1) { + if (tmp[0] == major) { + if (n == 1 || tmp[1] >= minor) + goto compare_version; + } + } + + /* else, this file does not qualify */ + continue; + + compare_version: + if (_cmpndewey(tmp, n, dewey, ndewey) <= 0) + continue; + + /* We have a better version */ + found_dot_so = 1; + snprintf(path, sizeof(path), "%s/%s", search_dirs[i], + dp->d_name); + found_dot_a = 0; + bcopy(tmp, dewey, sizeof(dewey)); + ndewey = n; + *majorp = dewey[0]; + *minorp = dewey[1]; + } + closedir(dd); + + if (found_dot_a || found_dot_so) + /* + * There's a lib in this dir; take it. + */ + return path[0] ? path : NULL; + } + + return path[0] ? path : NULL; +} + +void * +_citrus_find_getops(_citrus_module_t handle, const char *modname, + const char *ifname) +{ + char name[PATH_MAX]; + void *p; + + _DIAGASSERT(handle != NULL); + _DIAGASSERT(modname != NULL); + _DIAGASSERT(ifname != NULL); + + snprintf(name, sizeof(name), "_citrus_%s_%s_getops", modname, ifname); + p = dlsym((void *)handle, name); + return p; +} + +int +_citrus_load_module(_citrus_module_t *rhandle, const char *encname) +{ + const char *p; + char path[PATH_MAX]; + int maj, min; + void *handle; + + _DIAGASSERT(rhandle != NULL); + + if (_pathI18nModule == NULL) { + p = getenv("PATH_I18NMODULE"); + if (p != NULL && !issetugid()) { + _pathI18nModule = strdup(p); + if (_pathI18nModule == NULL) + return ENOMEM; + } else + _pathI18nModule = _PATH_I18NMODULE; + } + + (void)snprintf(path, sizeof(path), "lib%s", encname); + maj = I18NMODULE_MAJOR; + min = -1; + p = _findshlib(path, &maj, &min); + if (!p) + return (EINVAL); + handle = dlopen(p, RTLD_LAZY); + if (!handle) + return (EINVAL); + + *rhandle = (_citrus_module_t)handle; + + return (0); +} + +void +_citrus_unload_module(_citrus_module_t handle) +{ + if (handle) + dlclose((void *)handle); +} +#else +/* !_I18N_DYNAMIC */ + +void * +/*ARGSUSED*/ +_citrus_find_getops(_citrus_module_t handle, const char *modname, + const char *ifname) +{ + return (NULL); +} + +int +/*ARGSUSED*/ +_citrus_load_module(_citrus_module_t *rhandle, char const *modname) +{ + return (EINVAL); +} + +void +/*ARGSUSED*/ +_citrus_unload_module(_citrus_module_t handle) +{ +} +#endif diff --git a/lib/libc/citrus/citrus_module.h b/lib/libc/citrus/citrus_module.h new file mode 100644 index 0000000000..84b8493fdc --- /dev/null +++ b/lib/libc/citrus/citrus_module.h @@ -0,0 +1,44 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_module.h,v 1.1 2002/03/17 22:14:20 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_module.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#ifndef _CITRUS_MODULE_H_ +#define _CITRUS_MODULE_H_ + +typedef struct _citrus_module_rec *_citrus_module_t; + +__BEGIN_DECLS +void *_citrus_find_getops(_citrus_module_t __restrict, + const char * __restrict, const char * __restrict); +int _citrus_load_module(_citrus_module_t * __restrict, + const char * __restrict); +void _citrus_unload_module(_citrus_module_t); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/citrus_namespace.h b/lib/libc/citrus/citrus_namespace.h new file mode 100644 index 0000000000..92a12d1415 --- /dev/null +++ b/lib/libc/citrus/citrus_namespace.h @@ -0,0 +1,224 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_namespace.h,v 1.4 2004/07/21 14:16:34 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_namespace.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_NAMESPACE_H_ +#define _CITRUS_NAMESPACE_H_ + +/* citrus_alias */ +#ifndef _CITRUS_ALIAS_NO_NAMESPACE +#define _alias_lookup _citrus_alias_lookup +#endif /* _CITRUS_ALIAS_NO_NAMESPACE */ + +/* citrus_bcs */ +#ifndef _CITRUS_BCS_NO_NAMESPACE +#define _bcs_isalnum _citrus_bcs_isalnum +#define _bcs_isalpha _citrus_bcs_isalpha +#define _bcs_isblank _citrus_bcs_isblank +#define _bcs_isdigit _citrus_bcs_isdigit +#define _bcs_islower _citrus_bcs_islower +#define _bcs_iseol _citrus_bcs_iseol +#define _bcs_isspace _citrus_bcs_isspace +#define _bcs_isupper _citrus_bcs_isupper +#define _bcs_isxdigit _citrus_bcs_isxdigit +#define _bcs_skip_nonws _citrus_bcs_skip_nonws +#define _bcs_skip_nonws_len _citrus_bcs_skip_nonws_len +#define _bcs_skip_ws _citrus_bcs_skip_ws +#define _bcs_skip_ws_len _citrus_bcs_skip_ws_len +#define _bcs_strcasecmp _citrus_bcs_strcasecmp +#define _bcs_strncasecmp _citrus_bcs_strncasecmp +#define _bcs_tolower _citrus_bcs_tolower +#define _bcs_toupper _citrus_bcs_toupper +#define _bcs_trunc_rws_len _citrus_bcs_trunc_rws_len +#define _bcs_convert_to_lower _citrus_bcs_convert_to_lower +#define _bcs_convert_to_upper _citrus_bcs_convert_to_upper +#endif /* _CITRUS_BCS_NO_NAMESPACE */ + +/* citrus_csmapper */ +#ifndef _CITRUS_CSMAPPER_NO_NAMESPACE +#define _csmapper _citrus_csmapper +#define _csmapper_open _citrus_csmapper_open +#define _csmapper_close _citrus_csmapper_close +#define _csmapper_convert _citrus_csmapper_convert +#define _csmapper_init_state _citrus_csmapper_init_state +#define _csmapper_get_state_size _citrus_csmapper_get_state_size +#define _csmapper_get_src_max _citrus_csmapper_get_src_max +#define _csmapper_get_dst_max _citrus_csmapper_get_dst_max +#define _CSMAPPER_F_PREVENT_PIVOT _CITRUS_CSMAPPER_F_PREVENT_PIVOT +#endif /* _CITRUS_CSMAPPER_NO_NAMESPACE */ + +/* citrus_db */ +#ifndef _CITRUS_DB_NO_NAMESPACE +#define _db_open _citrus_db_open +#define _db_close _citrus_db_close +#define _db_lookup _citrus_db_lookup +#define _db_lookup_by_s _citrus_db_lookup_by_string +#define _db_lookup8_by_s _citrus_db_lookup8_by_string +#define _db_lookup16_by_s _citrus_db_lookup16_by_string +#define _db_lookup32_by_s _citrus_db_lookup32_by_string +#define _db_lookupstr_by_s _citrus_db_lookup_string_by_string +#define _db_hash_std _citrus_db_hash_std +#define _db_get_num_entries _citrus_db_get_number_of_entries +#define _db_get_entry _citrus_db_get_entry +#define _db_locator _citrus_db_locator +#define _db_locator_init _citrus_db_locator_init +#endif /* _CITRUS_DB_NO_NAMESPACE */ + +/* citrus_db_factory */ +#ifndef _CITRUS_DB_FACTORY_NO_NAMESPACE +#define _db_factory _citrus_db_factory +#define _db_factory_create _citrus_db_factory_create +#define _db_factory_free _citrus_db_factory_free +#define _db_factory_add _citrus_db_factory_add +#define _db_factory_add_by_s _citrus_db_factory_add_by_string +#define _db_factory_add8_by_s _citrus_db_factory_add8_by_string +#define _db_factory_add16_by_s _citrus_db_factory_add16_by_string +#define _db_factory_add32_by_s _citrus_db_factory_add32_by_string +#define _db_factory_addstr_by_s _citrus_db_factory_add_string_by_string +#define _db_factory_calc_size _citrus_db_factory_calc_size +#define _db_factory_serialize _citrus_db_factory_serialize +#endif /* _CITRUS_DB_FACTORY_NO_NAMESPACE */ + +/* citrus_lookup */ +#ifndef _CITRUS_DB_NO_NAMESPACE +#define _LOOKUP_CASE_SENSITIVE _CITRUS_LOOKUP_CASE_SENSITIVE +#define _LOOKUP_CASE_IGNORE _CITRUS_LOOKUP_CASE_IGNORE +#define _lookup _citrus_lookup +#define _lookup_simple _citrus_lookup_simple +#define _lookup_alias _citrus_lookup_alias +#define _lookup_seq_open _citrus_lookup_seq_open +#define _lookup_seq_rewind _citrus_lookup_seq_rewind +#define _lookup_seq_next _citrus_lookup_seq_next +#define _lookup_seq_lookup _citrus_lookup_seq_lookup +#define _lookup_get_num_entries _citrus_lookup_get_number_of_entries +#define _lookup_seq_close _citrus_lookup_seq_close +#define _lookup_factory_convert _citrus_lookup_factory_convert +#endif /* _CITRUS_DB_NO_NAMESPACE */ + +/* citrus_esdb */ +#ifndef _CITRUS_ESDB_NO_NAMESPACE +#define _esdb _citrus_esdb +#define _esdb_charset _citrus_esdb_charset +#define _esdb_open _citrus_esdb_open +#define _esdb_close _citrus_esdb_close +#define _esdb_get_list _citrus_esdb_get_list +#define _esdb_free_list _citrus_esdb_free_list +#endif /* _CITRUS_ESDB_NO_NAMESPACE */ + +/* citrus_hash */ +#ifndef _CITRUS_HASH_NO_NAMESPACE +#define _citrus_string_hash_func _string_hash_func +#endif /* _CITRUS_HASH_NO_NAMESPACE */ + +/* citrus_mapper */ +#ifndef _CITRUS_MAPPER_NO_NAMESPACE +#define _mapper _citrus_mapper +#define _mapper_ops _citrus_mapper_ops +#define _mapper_traits _citrus_mapper_traits +#define _mapper_open _citrus_mapper_open +#define _mapper_open_direct _citrus_mapper_open_direct +#define _mapper_close _citrus_mapper_close +#define _MAPPER_CONVERT_SUCCESS _CITRUS_MAPPER_CONVERT_SUCCESS +#define _MAPPER_CONVERT_NONIDENTICAL _CITRUS_MAPPER_CONVERT_NONIDENTICAL +#define _MAPPER_CONVERT_SRC_MORE _CITRUS_MAPPER_CONVERT_SRC_MORE +#define _MAPPER_CONVERT_DST_MORE _CITRUS_MAPPER_CONVERT_DST_MORE +#define _MAPPER_CONVERT_ILSEQ _CITRUS_MAPPER_CONVERT_ILSEQ +#define _MAPPER_CONVERT_FATAL _CITRUS_MAPPER_CONVERT_FATAL +#define _mapper_convert _citrus_mapper_convert +#define _mapper_init_state _citrus_mapper_init_state +#define _mapper_get_state_size _citrus_mapper_get_state_size +#define _mapper_get_src_max _citrus_mapper_get_src_max +#define _mapper_get_dst_max _citrus_mapper_get_dst_max +#define _mapper_set_persistent _citrus_mapper_set_persistent +#endif /* _CITRUS_MAPPER_NO_NAMESPACE */ + +/* citrus_memstream */ +#ifndef _CITRUS_MEMSTREAM_NO_NAMESPACE +#define _memstream _citrus_memory_stream +#define _memstream_getln _citrus_memory_stream_getln +#define _memstream_matchline _citrus_memory_stream_matchline +#define _memstream_chr _citrus_memory_stream_chr +#define _memstream_skip_ws _citrus_memory_stream_skip_ws +#define _memstream_iseof _citrus_memory_stream_iseof +#define _memstream_bind _citrus_memory_stream_bind +#define _memstream_seek _citrus_memory_stream_seek +#define _memstream_rewind _citrus_memory_stream_rewind +#define _memstream_remainder _citrus_memory_stream_remainder +#define _memstream_getc _citrus_memory_stream_getc +#define _memstream_peek _citrus_memory_stream_peek +#define _memstream_getregion _citrus_memory_stream_getregion +#endif /* _CITRUS_MEMSTREAM_NO_NAMESPACE */ + +/* citrus_mmap */ +#ifndef _CITRUS_MMAP_NO_NAMESPACE +#define _map_file _citrus_map_file +#define _unmap_file _citrus_unmap_file +#endif /* _CITRUS_MMAP_NO_NAMESPACE */ + +#ifndef _CITRUS_PIVOT_NO_NAMESPACE +#define _pivot_factory_convert _citrus_pivot_factory_convert +#endif /* _CITRUS_PIVOT_NO_NAMESPACE */ + +/* citrus_region.h */ +#ifndef _CITRUS_REGION_NO_NAMESPACE +#define _region _citrus_region +#define _region_init _citrus_region_init +#define _region_head _citrus_region_head +#define _region_size _citrus_region_size +#define _region_check _citrus_region_check +#define _region_offset _citrus_region_offset +#define _region_peek8 _citrus_region_peek8 +#define _region_peek16 _citrus_region_peek16 +#define _region_peek32 _citrus_region_peek32 +#endif /* _CITRUS_REGION_NO_NAMESPACE */ + +/* citrus_stdenc.h */ +#ifndef _CITRUS_STDENC_NO_NAMESPACE +#define _stdenc _citrus_stdenc +#define _stdenc_ops _citrus_stdenc_ops +#define _stdenc_traits _citrus_stdenc_traits +#define _stdenc_open _citrus_stdenc_open +#define _stdenc_close _citrus_stdenc_close +#define _stdenc_init_state _citrus_stdenc_init_state +#define _stdenc_mbtocs _citrus_stdenc_mbtocs +#define _stdenc_cstomb _citrus_stdenc_cstomb +#define _stdenc_mbtowc _citrus_stdenc_mbtowc +#define _stdenc_wctomb _citrus_stdenc_wctomb +#define _stdenc_put_state_reset _citrus_stdenc_put_state_reset +#define _stdenc_get_state_size _citrus_stdenc_get_state_size +#define _stdenc_get_mb_cur_max _citrus_stdenc_get_mb_cur_max +#endif /* _CITRUS_STDENC_NO_NAMESPACE */ + +/* citrus_types.h */ +#ifndef _CITRUS_TYPES_NO_NAMESPACE +#define _index_t _citrus_index_t +#define _csid_t _citrus_csid_t +#define _wc_t _citrus_wc_t +#endif /* _CITRUS_TYPES_NO_NAMESPACE */ + +#endif diff --git a/lib/libc/citrus/citrus_none.c b/lib/libc/citrus/citrus_none.c new file mode 100644 index 0000000000..d3fc6a7843 --- /dev/null +++ b/lib/libc/citrus/citrus_none.c @@ -0,0 +1,476 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_none.c,v 1.12 2004/01/18 03:57:30 yamt Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_none.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_none.h" +#include "citrus_stdenc.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_CTYPE_DECLS(NONE); +_CITRUS_CTYPE_DEF_OPS(NONE); + + +/* ---------------------------------------------------------------------- */ + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_init(void ** __restrict cl, void * __restrict var, + size_t lenvar, size_t lenps) +{ + *cl = NULL; + return (0); +} + +static void +/*ARGSUSED*/ +_citrus_NONE_ctype_uninit(void *cl) +{ +} + +static unsigned +/*ARGSUSED*/ +_citrus_NONE_ctype_get_mb_cur_max(void *cl) +{ + return (1); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mblen(void * __restrict cl, const char * __restrict s, + size_t n, int * __restrict nresult) +{ + if (!s) { + *nresult = 0; /* state independent */ + return (0); + } + if (n==0) { + *nresult = -1; + return (EILSEQ); + } + *nresult = (*s == 0) ? 0 : 1; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mbrlen(void * __restrict cl, const char * __restrict s, + size_t n, void * __restrict pspriv, + size_t * __restrict nresult) +{ + if (!s) { + *nresult = 0; + return (0); + } + if (n==0) { + *nresult = (size_t)-2; + return (0); + } + *nresult = (*s == 0) ? 0 : 1; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mbrtowc(void * __restrict cl, wchar_t * __restrict pwc, + const char * __restrict s, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + if (s == NULL) { + *nresult = 0; + return (0); + } + if (n == 0) { + *nresult = (size_t)-2; + return (0); + } + + if (pwc != NULL) + *pwc = (wchar_t)(unsigned char) *s; + + *nresult = *s == '\0' ? 0 : 1; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mbsinit(void * __restrict cl, + const void * __restrict pspriv, + int * __restrict nresult) +{ + *nresult = 1; /* always initial state */ + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mbsrtowcs(void * __restrict cl, wchar_t * __restrict pwcs, + const char ** __restrict s, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + int cnt; + const char *s0; + + /* if pwcs is NULL, ignore n */ + if (pwcs == NULL) + n = 1; /* arbitrary >0 value */ + + cnt = 0; + s0 = *s; /* to keep *s unchanged for now, use copy instead. */ + while (n > 0) { + if (pwcs != NULL) { + *pwcs = (wchar_t)(unsigned char)*s0; + } + if (*s0 == '\0') { + s0 = NULL; + break; + } + s0++; + if (pwcs != NULL) { + pwcs++; + n--; + } + cnt++; + } + if (pwcs) + *s = s0; + + *nresult = (size_t)cnt; + + return (0); +} + +static int +_citrus_NONE_ctype_mbstowcs(void * __restrict cl, wchar_t * __restrict wcs, + const char * __restrict s, size_t n, + size_t * __restrict nresult) +{ + return (_citrus_NONE_ctype_mbsrtowcs(cl, wcs, (const char **)&s, n, NULL, nresult)); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_mbtowc(void * __restrict cl, wchar_t * __restrict pwc, + const char * __restrict s, size_t n, + int * __restrict nresult) +{ + + if (s == NULL) { + *nresult = 0; /* state independent */ + return (0); + } + if (n == 0) { + return (EILSEQ); + } + if (pwc == NULL) { + if (*s == '\0') { + *nresult = 0; + } else { + *nresult = 1; + } + return (0); + } + + *pwc = (wchar_t)*s; + *nresult = *s == '\0' ? 0 : 1; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_wcrtomb(void * __restrict cl, char * __restrict s, + wchar_t wc, void * __restrict pspriv, + size_t * __restrict nresult) +{ + if ((wc&~0xFFU) != 0) { + *nresult = (size_t)-1; + return (EILSEQ); + } + + *nresult = 1; + if (s!=NULL) + *s = (char)wc; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_wcsrtombs(void * __restrict cl, char * __restrict s, + const wchar_t ** __restrict pwcs, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + size_t count; + const wchar_t *pwcs0; + + pwcs0 = *pwcs; + count = 0; + + if (s == NULL) + n = 1; + + while (n > 0) { + if ((*pwcs0 & ~0xFFU) != 0) { + *nresult = (size_t)-1; + return (EILSEQ); + } + if (s != NULL) { + *s++ = (char)*pwcs0; + n--; + } + if (*pwcs0 == L'\0') { + pwcs0 = NULL; + break; + } + count++; + pwcs0++; + } + if (s != NULL) + *pwcs = pwcs0; + + *nresult = count; + + return (0); +} + +static int +_citrus_NONE_ctype_wcstombs(void * __restrict cl, char * __restrict s, + const wchar_t * __restrict pwcs, size_t n, + size_t * __restrict nresult) +{ + return (_citrus_NONE_ctype_wcsrtombs(cl, s, (const wchar_t **)&pwcs, n, NULL, nresult)); +} + +static int +_citrus_NONE_ctype_wctomb(void * __restrict cl, char * __restrict s, + wchar_t wc, int * __restrict nresult) +{ + int ret; + size_t nr; + + if (s == 0) { + /* + * initialize state here. + * (nothing to do for us.) + */ + *nresult = 0; /* we're state independent */ + return (0); + } + + ret = _citrus_NONE_ctype_wcrtomb(cl, s, wc, NULL, &nr); + *nresult = (int)nr; + + return (ret); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_btowc(_citrus_ctype_rec_t * __restrict cc, + int c, wint_t * __restrict wcresult) +{ + if (c == EOF || c & ~0xFF) + *wcresult = WEOF; + else + *wcresult = (wint_t)c; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_ctype_wctob(_citrus_ctype_rec_t * __restrict cc, + wint_t wc, int * __restrict cresult) +{ + if (wc == WEOF || wc & ~0xFF) + *cresult = EOF; + else + *cresult = (int)wc; + return (0); +} + +/* ---------------------------------------------------------------------- */ + +_CITRUS_STDENC_DECLS(NONE); +_CITRUS_STDENC_DEF_OPS(NONE); +struct _citrus_stdenc_traits _citrus_NONE_stdenc_traits = { + 0, /* et_state_size */ + 1, /* mb_cur_max */ +}; + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_init(struct _citrus_stdenc * __restrict ce, + const void *var, size_t lenvar, + struct _citrus_stdenc_traits * __restrict et) +{ + + et->et_state_size = 0; + et->et_mb_cur_max = 1; + + ce->ce_closure = NULL; + + return (0); +} + +static void +/*ARGSUSED*/ +_citrus_NONE_stdenc_uninit(struct _citrus_stdenc *ce) +{ +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_init_state(struct _citrus_stdenc * __restrict ce, + void * __restrict ps) +{ + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_mbtocs(struct _citrus_stdenc * __restrict ce, + _csid_t *csid, _index_t *idx, + const char **s, size_t n, + void *ps, size_t *nresult) +{ + + _DIAGASSERT(csid != NULL && idx != NULL); + + if (n<1) { + *nresult = (size_t)-2; + return (0); + } + + *csid = 0; + *idx = (_index_t)(unsigned char)*(*s)++; + *nresult = *idx == 0 ? 0 : 1; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_cstomb(struct _citrus_stdenc * __restrict ce, + char *s, size_t n, + _csid_t csid, _index_t idx, + void *ps, size_t *nresult) +{ + + if (csid == _CITRUS_CSID_INVALID) { + *nresult = 0; + return (0); + } + if (n<1) { + *nresult = (size_t)-1; + return (E2BIG); + } + if (csid != 0 || (idx&0xFF) != idx) + return (EILSEQ); + + *s = (char)idx; + *nresult = 1; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_mbtowc(struct _citrus_stdenc * __restrict ce, + _wc_t * __restrict pwc, + const char ** __restrict s, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + if (s == NULL) { + *nresult = 0; + return (0); + } + if (n == 0) { + *nresult = (size_t)-2; + return (0); + } + + if (pwc != NULL) + *pwc = (_wc_t)(unsigned char) **s; + + *nresult = *s == '\0' ? 0 : 1; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_wctomb(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + _wc_t wc, void * __restrict pspriv, + size_t * __restrict nresult) +{ + if ((wc&~0xFFU) != 0) { + *nresult = (size_t)-1; + return (EILSEQ); + } + if (n==0) { + *nresult = (size_t)-1; + return (E2BIG); + } + + *nresult = 1; + if (s!=NULL && n>0) + *s = (char)wc; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_NONE_stdenc_put_state_reset(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + void * __restrict pspriv, + size_t * __restrict nresult) +{ + + *nresult = 0; + + return (0); +} diff --git a/lib/libc/citrus/citrus_none.h b/lib/libc/citrus/citrus_none.h new file mode 100644 index 0000000000..e111a39b7f --- /dev/null +++ b/lib/libc/citrus/citrus_none.h @@ -0,0 +1,37 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_none.h,v 1.3 2003/06/25 09:51:38 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_none.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_NONE_H_ +#define _CITRUS_NONE_H_ + +extern struct _citrus_ctype_ops_rec _citrus_NONE_ctype_ops; +extern struct _citrus_stdenc_ops _citrus_NONE_stdenc_ops; +extern struct _citrus_stdenc_traits _citrus_NONE_stdenc_traits; + +#endif diff --git a/lib/libc/citrus/citrus_pivot_factory.c b/lib/libc/citrus/citrus_pivot_factory.c new file mode 100644 index 0000000000..963abbe0cb --- /dev/null +++ b/lib/libc/citrus/citrus_pivot_factory.c @@ -0,0 +1,222 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_pivot_factory.c,v 1.4 2004/01/02 21:49:35 itojun Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_pivot_factory.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_region.h" +#include "citrus_bcs.h" +#include "citrus_db_factory.h" +#include "citrus_db_hash.h" +#include "citrus_pivot_file.h" +#include "citrus_pivot_factory.h" + +struct src_entry { + char *se_name; + struct _citrus_db_factory *se_df; + STAILQ_ENTRY(src_entry) se_entry; +}; +STAILQ_HEAD(src_head, src_entry); + +static int +find_src(struct src_head *sh, struct src_entry **rse, const char *name) +{ + int ret; + struct src_entry *se; + + STAILQ_FOREACH(se, sh, se_entry) { + if (_bcs_strcasecmp(se->se_name, name) == 0) { + *rse = se; + return 0; + } + } + se = malloc(sizeof(*se)); + if (se == NULL) + return errno; + se->se_name = strdup(name); + if (se->se_name == NULL) { + ret = errno; + free(se); + return ret; + } + ret = _db_factory_create(&se->se_df, &_db_hash_std, NULL); + if (ret) { + free(se->se_name); + free(se); + return ret; + } + STAILQ_INSERT_TAIL(sh, se, se_entry); + *rse = se; + + return 0; +} + +static void +free_src(struct src_head *sh) +{ + struct src_entry *se; + + while ((se = STAILQ_FIRST(sh)) != NULL) { + STAILQ_REMOVE_HEAD(sh, se_entry); + _db_factory_free(se->se_df); + free(se->se_name); + free(se); + } +} + + +#define T_COMM '#' +static int +convert_line(struct src_head *sh, const char *line, size_t len) +{ + int ret; + struct src_entry *se; + const char *p; + char key1[LINE_MAX], key2[LINE_MAX], data[LINE_MAX]; + uint32_t val; + + /* cut off trailing comment */ + p = memchr(line, T_COMM, len); + if (p) + len = p - line; + + /* key1 */ + line = _bcs_skip_ws_len(line, &len); + if (len == 0) + return 0; + p = _bcs_skip_nonws_len(line, &len); + if (p==line) + return 0; + snprintf(key1, sizeof(key1), "%.*s", (int)(p-line), line); + + /* key2 */ + line = _bcs_skip_ws_len(p, &len); + if (len == 0) + return 0; + p = _bcs_skip_nonws_len(line, &len); + if (p==line) + return 0; + snprintf(key2, sizeof(key2), "%.*s", (int)(p-line), line); + + /* data */ + line = _bcs_skip_ws_len(p, &len); + _bcs_trunc_rws_len(line, &len); + snprintf(data, sizeof(data), "%.*s", (int)len, line); + /* LINTED: discard const */ + val = strtoul(data, __DECONST(char **, &p), 0); + if (*p != '\0') + return EFTYPE; + + /* insert to DB */ + ret = find_src(sh, &se, key1); + if (ret) + return ret; + + return _db_factory_add32_by_s(se->se_df, key2, val); +} + +static int +dump_db(struct src_head *sh, struct _region *r) +{ + int ret; + struct _db_factory *df; + struct src_entry *se; + size_t size; + void *ptr; + struct _region subr; + + ret = _db_factory_create(&df, &_db_hash_std, NULL); + if (ret) + return ret; + + STAILQ_FOREACH(se, sh, se_entry) { + size = _db_factory_calc_size(se->se_df); + ptr = malloc(size); + if (ptr == NULL) + goto quit; + _region_init(&subr, ptr, size); + ret = _db_factory_serialize(se->se_df, _CITRUS_PIVOT_SUB_MAGIC, + &subr); + if (ret) + goto quit; + ret = _db_factory_add_by_s(df, se->se_name, &subr, 1); + if (ret) + goto quit; + } + + size = _db_factory_calc_size(df); + ptr = malloc(size); + if (ptr == NULL) + goto quit; + _region_init(r, ptr, size); + + ret = _db_factory_serialize(df, _CITRUS_PIVOT_MAGIC, r); + ptr = NULL; + +quit: + free(ptr); + _db_factory_free(df); + return ret; +} + +int +_citrus_pivot_factory_convert(FILE *out, FILE *in) +{ + struct src_head sh; + struct _region r; + char *line; + size_t size; + int ret; + + STAILQ_INIT(&sh); + + while ((line = fgetln(in, &size)) != NULL) + if ((ret = convert_line(&sh, line, size))) { + free_src(&sh); + return ret; + } + + ret = dump_db(&sh, &r); + free_src(&sh); + if (ret) + return ret; + + if (fwrite(_region_head(&r), _region_size(&r), 1, out) != 1) + return errno; + + return 0; +} diff --git a/lib/libc/citrus/citrus_pivot_factory.h b/lib/libc/citrus/citrus_pivot_factory.h new file mode 100644 index 0000000000..913a038327 --- /dev/null +++ b/lib/libc/citrus/citrus_pivot_factory.h @@ -0,0 +1,37 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_pivot_factory.h,v 1.1 2003/06/25 09:51:39 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_pivot_factory.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_PIVOT_FACTORY_H_ +#define _CITRUS_PIVOT_FACTORY_H_ + +__BEGIN_DECLS +int _citrus_pivot_factory_convert(FILE *, FILE *); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/citrus_pivot_file.h b/lib/libc/citrus/citrus_pivot_file.h new file mode 100644 index 0000000000..2a1fabbfdd --- /dev/null +++ b/lib/libc/citrus/citrus_pivot_file.h @@ -0,0 +1,36 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_pivot_file.h,v 1.1 2003/06/25 09:51:39 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_pivot_file.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_PIVOT_FILE_H_ +#define _CITRUS_PIVOT_FILE_H_ + +#define _CITRUS_PIVOT_MAGIC "CSPIVOT\0" +#define _CITRUS_PIVOT_SUB_MAGIC "CSPIVSUB" + +#endif diff --git a/lib/libc/citrus/citrus_region.h b/lib/libc/citrus/citrus_region.h new file mode 100644 index 0000000000..d4cd3a06c5 --- /dev/null +++ b/lib/libc/citrus/citrus_region.h @@ -0,0 +1,94 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_region.h,v 1.5 2004/12/30 05:00:42 christos Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_region.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#ifndef _CITRUS_REGION_H_ +#define _CITRUS_REGION_H_ + +struct _citrus_region { +/* private: */ + void *r_head; + size_t r_size; +}; + +static __inline void +_citrus_region_init(struct _citrus_region *r, void *h, size_t sz) +{ + _DIAGASSERT(r); + r->r_head = h; + r->r_size = sz; +} + +static __inline void * +_citrus_region_head(const struct _citrus_region *r) +{ + return r->r_head; +} + +static __inline size_t +_citrus_region_size(const struct _citrus_region *r) +{ + return r->r_size; +} + +static __inline int +_citrus_region_check(const struct _citrus_region *r, size_t ofs, size_t sz) +{ + return r->r_size >= ofs + sz ? 0 : -1; +} + +static __inline void * +_citrus_region_offset(const struct _citrus_region *r, size_t pos) +{ + return (void *)((uint8_t *)r->r_head + pos); +} + +static __inline uint8_t +_citrus_region_peek8(const struct _citrus_region *r, size_t pos) +{ + return *(uint8_t *)_citrus_region_offset(r, pos); +} + +static __inline uint16_t +_citrus_region_peek16(const struct _citrus_region *r, size_t pos) +{ + uint16_t val; + memcpy(&val, _citrus_region_offset(r, pos), 2); + return val; +} + +static __inline uint32_t +_citrus_region_peek32(const struct _citrus_region *r, size_t pos) +{ + uint32_t val; + memcpy(&val, _citrus_region_offset(r, pos), 4); + return val; +} + +#endif diff --git a/lib/libc/citrus/citrus_stdenc.c b/lib/libc/citrus/citrus_stdenc.c new file mode 100644 index 0000000000..701851f668 --- /dev/null +++ b/lib/libc/citrus/citrus_stdenc.c @@ -0,0 +1,179 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_stdenc.c,v 1.2 2003/07/10 08:50:44 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_stdenc.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_stdenc.h" +#include "citrus_none.h" + +struct _citrus_stdenc _citrus_stdenc_default = { + &_citrus_NONE_stdenc_ops, /* ce_ops */ + NULL, /* ce_closure */ + NULL, /* ce_module */ + &_citrus_NONE_stdenc_traits, /* ce_traits */ +}; + +#ifdef _I18N_DYNAMIC + +int +_citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict rce, + char const * __restrict encname, + const void * __restrict variable, size_t lenvar) +{ + int ret; + _citrus_module_t handle; + struct _citrus_stdenc *ce; + _citrus_stdenc_getops_t getops; + + _DIAGASSERT(encname != NULL); + _DIAGASSERT(!lenvar || variable!=NULL); + _DIAGASSERT(rce != NULL); + + if (!strcmp(encname, _CITRUS_DEFAULT_STDENC_NAME)) { + *rce = &_citrus_stdenc_default; + return (0); + } + ce = malloc(sizeof(*ce)); + if (ce==NULL) { + ret = errno; + goto bad; + } + ce->ce_ops = NULL; + ce->ce_closure = NULL; + ce->ce_module = NULL; + ce->ce_traits = NULL; + + ret = _citrus_load_module(&handle, encname); + if (ret) + goto bad; + + ce->ce_module = handle; + + getops = + (_citrus_stdenc_getops_t)_citrus_find_getops(ce->ce_module, + encname, "stdenc"); + if (getops == NULL) { + ret = EINVAL; + goto bad; + } + + ce->ce_ops = (struct _citrus_stdenc_ops *)malloc(sizeof(*ce->ce_ops)); + if (ce->ce_ops == NULL) { + ret = errno; + goto bad; + } + + ret = (*getops)(ce->ce_ops, sizeof(*ce->ce_ops), + _CITRUS_STDENC_ABI_VERSION); + if (ret) + goto bad; + + /* If return ABI version is not expected, should fixup it */ + + /* validation check */ + if (ce->ce_ops->eo_init == NULL || + ce->ce_ops->eo_uninit == NULL || + ce->ce_ops->eo_init_state == NULL || + ce->ce_ops->eo_mbtocs == NULL || + ce->ce_ops->eo_cstomb == NULL || + ce->ce_ops->eo_mbtowc == NULL || + ce->ce_ops->eo_wctomb == NULL) + goto bad; + + /* allocate traits */ + ce->ce_traits = malloc(sizeof(*ce->ce_traits)); + if (ce->ce_traits == NULL) { + ret = errno; + goto bad; + } + /* init and get closure */ + ret = (*ce->ce_ops->eo_init)(ce, variable, lenvar, ce->ce_traits); + if (ret) + goto bad; + + *rce = ce; + + return (0); + +bad: + _citrus_stdenc_close(ce); + return (ret); +} + +void +_citrus_stdenc_close(struct _citrus_stdenc *ce) +{ + + _DIAGASSERT(ce != NULL); + + if (ce == &_citrus_stdenc_default) + return; + + if (ce->ce_module) { + if (ce->ce_ops) { + if (ce->ce_closure && ce->ce_ops->eo_uninit) + (*ce->ce_ops->eo_uninit)(ce); + free(ce->ce_ops); + } + free(ce->ce_traits); + _citrus_unload_module(ce->ce_module); + } + free(ce); +} + +#else +/* !_I18N_DYNAMIC */ + +int +/*ARGSUSED*/ +_citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict rce, + char const * __restrict encname, + const void * __restrict variable, size_t lenvar) +{ + if (!strcmp(encname, _CITRUS_DEFAULT_STDENC_NAME)) { + *rce = &_citrus_stdenc_default; + return (0); + } + return (EINVAL); +} + +void +/*ARGSUSED*/ +_citrus_stdenc_close(struct _citrus_stdenc *ce) +{ +} + +#endif diff --git a/lib/libc/citrus/citrus_stdenc.h b/lib/libc/citrus/citrus_stdenc.h new file mode 100644 index 0000000000..a0a046fb11 --- /dev/null +++ b/lib/libc/citrus/citrus_stdenc.h @@ -0,0 +1,119 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_stdenc.h,v 1.3 2003/07/10 08:50:44 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_stdenc.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#ifndef _CITRUS_STDENC_H_ +#define _CITRUS_STDENC_H_ + +struct _citrus_stdenc; +struct _citrus_stdenc_ops; +struct _citrus_stdenc_traits; + +#include "citrus_stdenc_local.h" + +__BEGIN_DECLS +int _citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict, + char const * __restrict, + const void * __restrict, size_t); +void _citrus_stdenc_close(struct _citrus_stdenc *); +__END_DECLS + +static __inline int +_citrus_stdenc_init_state(struct _citrus_stdenc * __restrict ce, + void * __restrict ps) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_init_state); + return (*ce->ce_ops->eo_init_state)(ce, ps); +} + +static __inline int +_citrus_stdenc_mbtocs(struct _citrus_stdenc * __restrict ce, + _citrus_csid_t * __restrict csid, + _citrus_index_t * __restrict idx, + const char ** __restrict s, size_t n, + void * __restrict ps, size_t * __restrict nresult) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_mbtocs); + return (*ce->ce_ops->eo_mbtocs)(ce, csid, idx, s, n, ps, nresult); +} + +static __inline int +_citrus_stdenc_cstomb(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + _citrus_csid_t csid, _citrus_index_t idx, + void * __restrict ps, size_t * __restrict nresult) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_cstomb); + return (*ce->ce_ops->eo_cstomb)(ce, s, n, csid, idx, ps, nresult); +} + +static __inline int +_citrus_stdenc_mbtowc(struct _citrus_stdenc * __restrict ce, + _citrus_wc_t * __restrict wc, + const char ** __restrict s, size_t n, + void * __restrict ps, size_t * __restrict nresult) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_mbtocs); + return (*ce->ce_ops->eo_mbtowc)(ce, wc, s, n, ps, nresult); +} + +static __inline int +_citrus_stdenc_wctomb(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, _citrus_wc_t wc, + void * __restrict ps, size_t * __restrict nresult) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_cstomb); + return (*ce->ce_ops->eo_wctomb)(ce, s, n, wc, ps, nresult); +} + +static __inline int +_citrus_stdenc_put_state_reset(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + void * __restrict ps, + size_t * __restrict nresult) +{ + _DIAGASSERT(ce && ce->ce_ops && ce->ce_ops->eo_put_state_reset); + return (*ce->ce_ops->eo_put_state_reset)(ce, s, n, ps, nresult); +} + +static __inline size_t +_citrus_stdenc_get_state_size(struct _citrus_stdenc *ce) +{ + _DIAGASSERT(ce && ce->ce_traits); + return ce->ce_traits->et_state_size; +} + +static __inline size_t +_citrus_stdenc_get_mb_cur_max(struct _citrus_stdenc *ce) +{ + _DIAGASSERT(ce && ce->ce_traits); + return ce->ce_traits->et_mb_cur_max; +} + +#endif diff --git a/lib/libc/citrus/citrus_stdenc_local.h b/lib/libc/citrus/citrus_stdenc_local.h new file mode 100644 index 0000000000..968a5ab976 --- /dev/null +++ b/lib/libc/citrus/citrus_stdenc_local.h @@ -0,0 +1,146 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_stdenc_local.h,v 1.2 2003/06/26 12:09:57 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_stdenc_local.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#ifndef _CITRUS_STDENC_LOCAL_H_ +#define _CITRUS_STDENC_LOCAL_H_ + +#define _CITRUS_STDENC_GETOPS_FUNC_BASE(n) \ +int n(struct _citrus_stdenc_ops *, size_t, uint32_t) +#define _CITRUS_STDENC_GETOPS_FUNC(_e_) \ +_CITRUS_STDENC_GETOPS_FUNC_BASE(_citrus_##_e_##_stdenc_getops) +typedef _CITRUS_STDENC_GETOPS_FUNC_BASE((*_citrus_stdenc_getops_t)); + + +#define _CITRUS_STDENC_DECLS(_e_) \ +static int _citrus_##_e_##_stdenc_init \ + (struct _citrus_stdenc * __restrict, const void * __restrict, \ + size_t, struct _citrus_stdenc_traits * __restrict); \ +static void _citrus_##_e_##_stdenc_uninit(struct _citrus_stdenc *); \ +static int _citrus_##_e_##_stdenc_init_state \ + (struct _citrus_stdenc * __restrict, void * __restrict); \ +static int _citrus_##_e_##_stdenc_mbtocs \ + (struct _citrus_stdenc * __restrict, \ + _citrus_csid_t * __restrict, _citrus_index_t * __restrict, \ + const char ** __restrict, size_t, \ + void * __restrict, size_t * __restrict); \ +static int _citrus_##_e_##_stdenc_cstomb \ + (struct _citrus_stdenc * __restrict, char * __restrict, \ + size_t, _citrus_csid_t, _citrus_index_t, \ + void * __restrict, size_t * __restrict); \ +static int _citrus_##_e_##_stdenc_mbtowc \ + (struct _citrus_stdenc * __restrict, \ + _citrus_wc_t * __restrict, \ + const char ** __restrict, size_t, \ + void * __restrict, size_t * __restrict); \ +static int _citrus_##_e_##_stdenc_wctomb \ + (struct _citrus_stdenc * __restrict, char * __restrict, size_t, \ + _citrus_wc_t, void * __restrict, size_t * __restrict); \ +static int _citrus_##_e_##_stdenc_put_state_reset \ + (struct _citrus_stdenc * __restrict, char * __restrict, size_t, \ + void * __restrict, size_t * __restrict) + +#define _CITRUS_STDENC_DEF_OPS(_e_) \ +struct _citrus_stdenc_ops _citrus_##_e_##_stdenc_ops = { \ + /* eo_abi_version */ _CITRUS_STDENC_ABI_VERSION, \ + /* eo_init */ &_citrus_##_e_##_stdenc_init, \ + /* eo_uninit */ &_citrus_##_e_##_stdenc_uninit, \ + /* eo_init_state */ &_citrus_##_e_##_stdenc_init_state, \ + /* eo_mbtocs */ &_citrus_##_e_##_stdenc_mbtocs, \ + /* eo_cstomb */ &_citrus_##_e_##_stdenc_cstomb, \ + /* eo_mbtowc */ &_citrus_##_e_##_stdenc_mbtowc, \ + /* eo_wctomb */ &_citrus_##_e_##_stdenc_wctomb, \ + /* eo_put_state_reset */&_citrus_##_e_##_stdenc_put_state_reset \ +} + +typedef int (*_citrus_stdenc_init_t) + (struct _citrus_stdenc * __reatrict, const void * __restrict , size_t, + struct _citrus_stdenc_traits * __restrict); +typedef void (*_citrus_stdenc_uninit_t)(struct _citrus_stdenc * __restrict); +typedef int (*_citrus_stdenc_init_state_t) + (struct _citrus_stdenc * __restrict, void * __restrict); +typedef int (*_citrus_stdenc_mbtocs_t) + (struct _citrus_stdenc * __restrict, + _citrus_csid_t * __restrict, _citrus_index_t * __restrict, + const char ** __restrict, size_t, + void * __restrict, size_t * __restrict); +typedef int (*_citrus_stdenc_cstomb_t) + (struct _citrus_stdenc *__restrict, char * __restrict, size_t, + _citrus_csid_t, _citrus_index_t, void * __restrict, + size_t * __restrict); +typedef int (*_citrus_stdenc_mbtowc_t) + (struct _citrus_stdenc * __restrict, + _citrus_wc_t * __restrict, + const char ** __restrict, size_t, + void * __restrict, size_t * __restrict); +typedef int (*_citrus_stdenc_wctomb_t) + (struct _citrus_stdenc *__restrict, char * __restrict, size_t, + _citrus_wc_t, void * __restrict, size_t * __restrict); +typedef int (*_citrus_stdenc_put_state_reset_t) + (struct _citrus_stdenc *__restrict, char * __restrict, size_t, + void * __restrict, size_t * __restrict); + +/* + * ABI version change log + * 0x00000001 + * initial version + */ +#define _CITRUS_STDENC_ABI_VERSION 0x00000001 +struct _citrus_stdenc_ops { + uint32_t eo_abi_version; + /* version 0x00000001 */ + _citrus_stdenc_init_t eo_init; + _citrus_stdenc_uninit_t eo_uninit; + _citrus_stdenc_init_state_t eo_init_state; + _citrus_stdenc_mbtocs_t eo_mbtocs; + _citrus_stdenc_cstomb_t eo_cstomb; + _citrus_stdenc_mbtowc_t eo_mbtowc; + _citrus_stdenc_wctomb_t eo_wctomb; + _citrus_stdenc_put_state_reset_t eo_put_state_reset; +}; + +struct _citrus_stdenc_traits { + /* version 0x00000001 */ + size_t et_state_size; + size_t et_mb_cur_max; +}; + +struct _citrus_stdenc { + /* public */ + /* version 0x00000001 */ + struct _citrus_stdenc_ops *ce_ops; + void *ce_closure; + /* private */ + _citrus_module_t ce_module; + struct _citrus_stdenc_traits *ce_traits; +}; + +#define _CITRUS_DEFAULT_STDENC_NAME "NONE" + +#endif diff --git a/lib/libc/citrus/citrus_stdenc_template.h b/lib/libc/citrus/citrus_stdenc_template.h new file mode 100644 index 0000000000..e65afbbe95 --- /dev/null +++ b/lib/libc/citrus/citrus_stdenc_template.h @@ -0,0 +1,187 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_stdenc_template.h,v 1.2 2003/06/26 12:09:57 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_stdenc_template.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * CAUTION: THIS IS NOT STANDALONE FILE + * + * function templates of iconv standard encoding handler for each encodings. + * + */ + +/* + * macros + */ + +#undef _TO_EI +#undef _CE_TO_EI +#undef _TO_STATE +#define _TO_EI(_cl_) ((_ENCODING_INFO*)(_cl_)) +#define _CE_TO_EI(_ce_) (_TO_EI((_ce_)->ce_closure)) +#define _TO_STATE(_ps_) ((_ENCODING_STATE*)(_ps_)) + +/* ---------------------------------------------------------------------- + * templates for public functions + */ + +int +_FUNCNAME(stdenc_getops)(struct _citrus_stdenc_ops *ops, size_t lenops, + uint32_t expected_version) +{ + if (expected_version<_CITRUS_STDENC_ABI_VERSION || lenops 0) { + ei = calloc(1, sizeof(_ENCODING_INFO)); + if (ei == NULL) { + return errno; + } + } + + ret = _FUNCNAME(encoding_module_init)(ei, var, lenvar); + if (ret) { + free((void *)ei); + return ret; + } + + ce->ce_closure = ei; + et->et_state_size = sizeof(_ENCODING_STATE); + et->et_mb_cur_max = _ENCODING_MB_CUR_MAX(_CE_TO_EI(ce)); + + return 0; +} + +static void +_FUNCNAME(stdenc_uninit)(struct _citrus_stdenc * __restrict ce) +{ + if (ce) { + _FUNCNAME(encoding_module_uninit)(_CE_TO_EI(ce)); + free(ce->ce_closure); + } +} + +static int +_FUNCNAME(stdenc_init_state)(struct _citrus_stdenc * __restrict ce, + void * __restrict ps) +{ + _FUNCNAME(init_state)(_CE_TO_EI(ce), _TO_STATE(ps)); + + return 0; +} + +static int +_FUNCNAME(stdenc_mbtocs)(struct _citrus_stdenc * __restrict ce, + _citrus_csid_t * __restrict csid, + _citrus_index_t * __restrict idx, + const char ** __restrict s, size_t n, + void * __restrict ps, size_t * __restrict nresult) +{ + int ret; + wchar_t wc; + + _DIAGASSERT(nresult != NULL); + + ret = _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), &wc, s, n, + _TO_STATE(ps), nresult); + + if (!ret && *nresult != (size_t)-2) + _FUNCNAME(stdenc_wctocs)(_CE_TO_EI(ce), csid, idx, wc); + + return ret; +} + +static int +_FUNCNAME(stdenc_cstomb)(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + _citrus_csid_t csid, _citrus_index_t idx, + void * __restrict ps, size_t * __restrict nresult) +{ + int ret; + wchar_t wc; + + _DIAGASSERT(nresult != NULL); + + wc = 0; + + if (csid != _CITRUS_CSID_INVALID) { + ret = _FUNCNAME(stdenc_cstowc)(_CE_TO_EI(ce), &wc, csid, idx); + if (ret) + return ret; + } + + return _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, _TO_STATE(ps), + nresult); +} + +static int +_FUNCNAME(stdenc_mbtowc)(struct _citrus_stdenc * __restrict ce, + _citrus_wc_t * __restrict wc, + const char ** __restrict s, size_t n, + void * __restrict ps, size_t * __restrict nresult) +{ + return _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), wc, s, n, + _TO_STATE(ps), nresult); +} + +static int +_FUNCNAME(stdenc_wctomb)(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, _citrus_wc_t wc, + void * __restrict ps, size_t * __restrict nresult) +{ + return _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, _TO_STATE(ps), + nresult); +} + +static int +_FUNCNAME(stdenc_put_state_reset)(struct _citrus_stdenc * __restrict ce, + char * __restrict s, size_t n, + void * __restrict ps, + size_t * __restrict nresult) +{ +#if _ENCODING_IS_STATE_DEPENDENT + return _FUNCNAME(put_state_reset)(_CE_TO_EI(ce), s, n, _TO_STATE(ps), + nresult); +#else + *nresult = 0; + return 0; +#endif +} diff --git a/lib/libc/citrus/citrus_types.h b/lib/libc/citrus/citrus_types.h new file mode 100644 index 0000000000..87c517b751 --- /dev/null +++ b/lib/libc/citrus/citrus_types.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/citrus_types.h,v 1.3 2003/10/27 00:12:42 lukem Exp $ */ +/* $DragonFly: src/lib/libc/citrus/citrus_types.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_TYPES_H_ +#define _CITRUS_TYPES_H_ + +typedef uint32_t _citrus_wc_t; +typedef uint32_t _citrus_index_t; +typedef uint32_t _citrus_csid_t; +#define _CITRUS_CSID_INVALID ((_citrus_csid_t)-1) + +#endif diff --git a/lib/libc/citrus/modules/citrus_big5.c b/lib/libc/citrus/modules/citrus_big5.c new file mode 100644 index 0000000000..85569a22d7 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_big5.c @@ -0,0 +1,395 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_big5.c,v 1.8 2003/08/07 16:42:38 agc Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_big5.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_big5.h" + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + char ch[2]; + int chlen; +} _BIG5State; + +typedef struct { + int dummy; +} _BIG5EncodingInfo; + +typedef struct { + _BIG5EncodingInfo ei; + struct { + /* for future multi-locale facility */ + _BIG5State s_mblen; + _BIG5State s_mbrlen; + _BIG5State s_mbrtowc; + _BIG5State s_mbtowc; + _BIG5State s_mbsrtowcs; + _BIG5State s_wcrtomb; + _BIG5State s_wcsrtombs; + _BIG5State s_wctomb; + } states; +} _BIG5CTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_BIG5_##m +#define _ENCODING_INFO _BIG5EncodingInfo +#define _CTYPE_INFO _BIG5CTypeInfo +#define _ENCODING_STATE _BIG5State +#define _ENCODING_MB_CUR_MAX(_ei_) 2 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static __inline void +/*ARGSUSED*/ +_citrus_BIG5_init_state(_BIG5EncodingInfo * __restrict ei, + _BIG5State * __restrict s) +{ + memset(s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_BIG5_pack_state(_BIG5EncodingInfo * __restrict ei, + void * __restrict pspriv, + const _BIG5State * __restrict s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_BIG5_unpack_state(_BIG5EncodingInfo * __restrict ei, + _BIG5State * __restrict s, + const void * __restrict pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static __inline int +_citrus_BIG5_check(u_int c) +{ + c &= 0xff; + return ((c >= 0xa1 && c <= 0xfe) ? 2 : 1); +} + +static __inline int +_citrus_BIG5_check2(u_int c) +{ + c &= 0xff; + if ((c >= 0x40 && c <= 0x7f) || (c >= 0xa1 && c <= 0xfe)) + return 1; + else + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_BIG5_encoding_module_init(_BIG5EncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + _DIAGASSERT(ei != NULL); + + memset((void *)ei, 0, sizeof(*ei)); + + return (0); +} + +static void +/*ARGSUSED*/ +_citrus_BIG5_encoding_module_uninit(_BIG5EncodingInfo *ei) +{ +} + +static int +/*ARGSUSED*/ +_citrus_BIG5_mbrtowc_priv(_BIG5EncodingInfo * __restrict ei, + wchar_t * __restrict pwc, + const char ** __restrict s, size_t n, + _BIG5State * __restrict psenc, + size_t * __restrict nresult) +{ + wchar_t wchar; + int c; + int chlenbak; + const char *s0; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s != NULL && *s != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_BIG5_init_state(ei, psenc); + *nresult = 0; + return (0); + } + + chlenbak = psenc->chlen; + + /* make sure we have the first byte in the buffer */ + switch (psenc->chlen) { + case 0: + if (n < 1) + goto restart; + psenc->ch[0] = *s0++; + psenc->chlen = 1; + n--; + break; + case 1: + break; + default: + /* illegal state */ + goto ilseq; + } + + c = _citrus_BIG5_check(psenc->ch[0] & 0xff); + if (c == 0) + goto ilseq; + while (psenc->chlen < c) { + if (n < 1) { + goto restart; + } + psenc->ch[psenc->chlen] = *s0++; + psenc->chlen++; + n--; + } + + switch (c) { + case 1: + wchar = psenc->ch[0] & 0xff; + break; + case 2: + if (!_citrus_BIG5_check2(psenc->ch[1] & 0xff)) + goto ilseq; + wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); + break; + default: + /* illegal state */ + goto ilseq; + } + + *s = s0; + psenc->chlen = 0; + if (pwc) + *pwc = wchar; + if (!wchar) + *nresult = 0; + else + *nresult = c - chlenbak; + + return (0); + +ilseq: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +restart: + *s = s0; + *nresult = (size_t)-2; + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_BIG5_wcrtomb_priv(_BIG5EncodingInfo * __restrict ei, + char * __restrict s, + size_t n, wchar_t wc, _BIG5State * __restrict psenc, + size_t * __restrict nresult) +{ + int l, ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + /* check invalid sequence */ + if (wc & ~0xffff) { + ret = EILSEQ; + goto err; + } + + if (wc & 0x8000) { + if (_citrus_BIG5_check((wc >> 8) & 0xff) != 2 || + !_citrus_BIG5_check2(wc & 0xff)) { + ret = EILSEQ; + goto err; + } + l = 2; + } else { + if (wc & ~0xff || !_citrus_BIG5_check(wc & 0xff)) { + ret = EILSEQ; + goto err; + } + l = 1; + } + + if (n < l) { + /* bound check failure */ + ret = E2BIG; + goto err; + } + + if (l == 2) { + s[0] = (wc >> 8) & 0xff; + s[1] = wc & 0xff; + } else + s[0] = wc & 0xff; + + *nresult = l; + + return 0; + +err: + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_BIG5_stdenc_wctocs(_BIG5EncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + + _DIAGASSERT(csid != NULL && idx != NULL); + + if (wc<0x100) + *csid = 0; + else + *csid = 1; + *idx = (_index_t)wc; + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_BIG5_stdenc_cstowc(_BIG5EncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + u_int8_t h, l; + + _DIAGASSERT(wc != NULL); + + switch (csid) { + case 0: + if (idx>=0x80U) + return EILSEQ; + *wc = (wchar_t)idx; + break; + case 1: + if (idx>=0x10000U) + return EILSEQ; + h = idx >> 8; + l = idx; + if (h<0xA1 || h>0xF9 || l<0x40 || l>0xFE) + return EILSEQ; + *wc = (wchar_t)idx; + break; + default: + return EILSEQ; + } + + return 0; +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(BIG5); +_CITRUS_CTYPE_DEF_OPS(BIG5); + +#include "citrus_ctype_template.h" + + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(BIG5); +_CITRUS_STDENC_DEF_OPS(BIG5); + +#include "citrus_stdenc_template.h" diff --git a/lib/libc/citrus/modules/citrus_big5.h b/lib/libc/citrus/modules/citrus_big5.h new file mode 100644 index 0000000000..e9813a3a34 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_big5.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_big5.h,v 1.2 2003/06/25 09:51:41 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_big5.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_BIG5_H_ +#define _CITRUS_BIG5_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(BIG5); +_CITRUS_STDENC_GETOPS_FUNC(BIG5); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_euc.c b/lib/libc/citrus/modules/citrus_euc.c new file mode 100644 index 0000000000..0de54a56ee --- /dev/null +++ b/lib/libc/citrus/modules/citrus_euc.c @@ -0,0 +1,414 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_euc.c,v 1.8 2003/08/07 16:42:38 agc Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_euc.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_euc.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + char ch[3]; + int chlen; +} _EUCState; + +typedef struct { + unsigned count[4]; + wchar_t bits[4]; + wchar_t mask; + unsigned mb_cur_max; +} _EUCEncodingInfo; + +typedef struct { + _EUCEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _EUCState s_mblen; + _EUCState s_mbrlen; + _EUCState s_mbrtowc; + _EUCState s_mbtowc; + _EUCState s_mbsrtowcs; + _EUCState s_wcrtomb; + _EUCState s_wcsrtombs; + _EUCState s_wctomb; + } states; +} _EUCCTypeInfo; + +#define _SS2 0x008e +#define _SS3 0x008f + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_EUC_##m +#define _ENCODING_INFO _EUCEncodingInfo +#define _CTYPE_INFO _EUCCTypeInfo +#define _ENCODING_STATE _EUCState +#define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static __inline int +_citrus_EUC_cs(unsigned int c) +{ + c &= 0xff; + + return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0); +} + +static __inline int +_citrus_EUC_parse_variable(_EUCEncodingInfo *ei, + const void *var, size_t lenvar) +{ + const char *v, *e; + int x; + + /* parse variable string */ + if (!var) + return (EFTYPE); + + v = (const char *) var; + + while (*v == ' ' || *v == '\t') + ++v; + + ei->mb_cur_max = 1; + for (x = 0; x < 4; ++x) { + ei->count[x] = (int) strtol(v, (char **)&e, 0); + if (v == e || !(v = e) || ei->count[x]<1 || ei->count[x]>4) { + return (EFTYPE); + } + if (ei->mb_cur_max < ei->count[x]) + ei->mb_cur_max = ei->count[x]; + while (*v == ' ' || *v == '\t') + ++v; + ei->bits[x] = (int) strtol(v, (char **)&e, 0); + if (v == e || !(v = e)) { + return (EFTYPE); + } + while (*v == ' ' || *v == '\t') + ++v; + } + ei->mask = (int)strtol(v, (char **)&e, 0); + if (v == e || !(v = e)) { + return (EFTYPE); + } + + return 0; +} + + +static __inline void +/*ARGSUSED*/ +_citrus_EUC_init_state(_EUCEncodingInfo *ei, _EUCState *s) +{ + memset(s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_EUC_pack_state(_EUCEncodingInfo *ei, void *pspriv, const _EUCState *s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_EUC_unpack_state(_EUCEncodingInfo *ei, _EUCState *s, + const void *pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static int +_citrus_EUC_mbrtowc_priv(_EUCEncodingInfo *ei, wchar_t *pwc, const char **s, + size_t n, _EUCState *psenc, size_t *nresult) +{ + wchar_t wchar; + int c, cs, len; + int chlenbak; + const char *s0, *s1 = NULL; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_EUC_init_state(ei, psenc); + *nresult = 0; /* state independent */ + return (0); + } + + chlenbak = psenc->chlen; + + /* make sure we have the first byte in the buffer */ + switch (psenc->chlen) { + case 0: + if (n < 1) + goto restart; + psenc->ch[0] = *s0++; + psenc->chlen = 1; + n--; + break; + case 1: + case 2: + break; + default: + /* illgeal state */ + goto encoding_error; + } + + c = ei->count[cs = _citrus_EUC_cs(psenc->ch[0] & 0xff)]; + if (c == 0) + goto encoding_error; + while (psenc->chlen < c) { + if (n < 1) + goto restart; + psenc->ch[psenc->chlen] = *s0++; + psenc->chlen++; + n--; + } + *s = s0; + + switch (cs) { + case 3: + case 2: + /* skip SS2/SS3 */ + len = c - 1; + s1 = &psenc->ch[1]; + break; + case 1: + case 0: + len = c; + s1 = &psenc->ch[0]; + break; + } + wchar = 0; + while (len-- > 0) + wchar = (wchar << 8) | (*s1++ & 0xff); + wchar = (wchar & ~ei->mask) | ei->bits[cs]; + + psenc->chlen = 0; + if (pwc) + *pwc = wchar; + + if (!wchar) { + *nresult = 0; + } else { + *nresult = (size_t)(c - chlenbak); + } + + return 0; + +encoding_error: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +restart: + *nresult = (size_t)-2; + *s = s0; + return (0); +} + +static int +_citrus_EUC_wcrtomb_priv(_EUCEncodingInfo *ei, char *s, size_t n, wchar_t wc, + _EUCState *psenc, size_t *nresult) +{ + wchar_t m, nm; + int cs, i, ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + m = wc & ei->mask; + nm = wc & ~m; + + for (cs = 0; + cs < sizeof(ei->count)/sizeof(ei->count[0]); + cs++) { + if (m == ei->bits[cs]) + break; + } + /* fallback case - not sure if it is necessary */ + if (cs == sizeof(ei->count)/sizeof(ei->count[0])) + cs = 1; + + i = ei->count[cs]; + if (n < i) { + ret = E2BIG; + goto err; + } + m = (cs % 2) ? 0x80 : 0x00; + switch (cs) { + case 2: + *s++ = _SS2; + i--; + break; + case 3: + *s++ = _SS3; + i--; + break; + } + + while (i-- > 0) + *s++ = ((nm >> (i << 3)) & 0xff) | m; + + *nresult = (size_t)ei->count[cs]; + return 0; + +err: + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + wchar_t m, nm; + + _DIAGASSERT(ei != NULL && csid != NULL && idx != NULL); + + m = wc & ei->mask; + nm = wc & ~m; + + *csid = (_citrus_csid_t)m; + *idx = (_citrus_index_t)nm; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(ei != NULL && wc != NULL); + + if ((csid & ~ei->mask) != 0 || (idx & ei->mask) != 0) + return (EINVAL); + + *wc = (wchar_t)csid | (wchar_t)idx; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + + _DIAGASSERT(ei != NULL); + + return (_citrus_EUC_parse_variable(ei, var, lenvar)); +} + +static void +/*ARGSUSED*/ +_citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei) +{ +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(EUC); +_CITRUS_CTYPE_DEF_OPS(EUC); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(EUC); +_CITRUS_STDENC_DEF_OPS(EUC); + +#include "citrus_stdenc_template.h" diff --git a/lib/libc/citrus/modules/citrus_euc.h b/lib/libc/citrus/modules/citrus_euc.h new file mode 100644 index 0000000000..922dd6e0fb --- /dev/null +++ b/lib/libc/citrus/modules/citrus_euc.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_euc.h,v 1.2 2003/06/25 09:51:42 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_euc.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_EUC_H_ +#define _CITRUS_EUC_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(EUC); +_CITRUS_STDENC_GETOPS_FUNC(EUC); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_euctw.c b/lib/libc/citrus/modules/citrus_euctw.c new file mode 100644 index 0000000000..24148c157f --- /dev/null +++ b/lib/libc/citrus/modules/citrus_euctw.c @@ -0,0 +1,413 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_euctw.c,v 1.8 2003/10/14 12:50:03 yamt Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_euctw.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/*- + * Copyright (c)1999 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_euctw.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + char ch[4]; + int chlen; +} _EUCTWState; + +typedef struct { + int dummy; +} _EUCTWEncodingInfo; +typedef struct { + _EUCTWEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _EUCTWState s_mblen; + _EUCTWState s_mbrlen; + _EUCTWState s_mbrtowc; + _EUCTWState s_mbtowc; + _EUCTWState s_mbsrtowcs; + _EUCTWState s_wcrtomb; + _EUCTWState s_wcsrtombs; + _EUCTWState s_wctomb; + } states; +} _EUCTWCTypeInfo; + +#define _SS2 0x008e +#define _SS3 0x008f + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_EUCTW_##m +#define _ENCODING_INFO _EUCTWEncodingInfo +#define _CTYPE_INFO _EUCTWCTypeInfo +#define _ENCODING_STATE _EUCTWState +#define _ENCODING_MB_CUR_MAX(_ei_) 4 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + +static __inline int +_citrus_EUCTW_cs(u_int c) +{ + c &= 0xff; + + return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0); +} + +static __inline int +_citrus_EUCTW_count(int cs) +{ + switch (cs) { + case 0: + return 1; + case 1: + return 2; + case 2: + return 4; + case 3: + abort(); + /*NOTREACHED*/ + } + return 0; +} + +static __inline void +/*ARGSUSED*/ +_citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei, + _EUCTWState * __restrict s) +{ + memset(s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei, + void * __restrict pspriv, + const _EUCTWState * __restrict s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei, + _EUCTWState * __restrict s, + const void * __restrict pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static int +/*ARGSUSED*/ +_citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + + _DIAGASSERT(ei != NULL); + + memset((void *)ei, 0, sizeof(*ei)); + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei) +{ +} + +static int +_citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, + const char ** __restrict s, + size_t n, _EUCTWState * __restrict psenc, + size_t * __restrict nresult) +{ + wchar_t wchar; + int c, cs; + int chlenbak; + const char *s0; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_EUCTW_init_state(ei, psenc); + *nresult = 0; /* state independent */ + return (0); + } + + chlenbak = psenc->chlen; + + /* make sure we have the first byte in the buffer */ + switch (psenc->chlen) { + case 0: + if (n < 1) + goto restart; + psenc->ch[0] = *s0++; + psenc->chlen = 1; + n--; + break; + case 1: + case 2: + break; + default: + /* illgeal state */ + goto ilseq; + } + + c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff)); + if (c == 0) + goto ilseq; + while (psenc->chlen < c) { + if (n < 1) + goto ilseq; + psenc->ch[psenc->chlen] = *s0++; + psenc->chlen++; + n--; + } + + wchar = 0; + switch (cs) { + case 0: + if (psenc->ch[0] & 0x80) + goto ilseq; + wchar = psenc->ch[0] & 0xff; + break; + case 1: + if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80)) + goto ilseq; + wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); + wchar |= 'G' << 24; + break; + case 2: + if ((u_char)psenc->ch[1] < 0xa1 || 0xa7 < (u_char)psenc->ch[1]) + goto ilseq; + if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80)) + goto ilseq; + wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff); + wchar |= ('G' + psenc->ch[1] - 0xa1) << 24; + break; + default: + goto ilseq; + } + + *s = s0; + psenc->chlen = 0; + + if (pwc) + *pwc = wchar; + + if (!wchar) + *nresult = 0; + else + *nresult = c - chlenbak; + + return (0); + +ilseq: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +restart: + *s = s0; + *nresult = (size_t)-1; + return (0); +} + +static int +_citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _EUCTWState * __restrict psenc, + size_t * __restrict nresult) +{ + wchar_t cs = wc & 0x7f000080; + wchar_t v; + int i, len, clen, ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + clen = 1; + if (wc & 0x00007f00) + clen = 2; + if ((wc & 0x007f0000) && !(wc & 0x00800000)) + clen = 3; + + if (clen == 1 && cs == 0x00000000) { + /* ASCII */ + len = 1; + if (n < len) { + ret = E2BIG; + goto err; + } + v = wc & 0x0000007f; + } else if (clen == 2 && cs == ('G' << 24)) { + /* CNS-11643-1 */ + len = 2; + if (n < len) { + ret = E2BIG; + goto err; + } + v = wc & 0x00007f7f; + v |= 0x00008080; + } else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') { + /* CNS-11643-[2-7] */ + len = 4; + if (n < len) { + ret = E2BIG; + goto err; + } + *s++ = _SS2; + *s++ = (cs >> 24) - 'H' + 0xa2; + v = wc & 0x00007f7f; + v |= 0x00008080; + } else { + ret = EILSEQ; + goto err; + } + + i = clen; + while (i-- > 0) + *s++ = (v >> (i << 3)) & 0xff; + + *nresult = len; + return 0; + +err: + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + + _DIAGASSERT(ei != NULL && csid != NULL && idx != NULL); + + *csid = (_csid_t)(wc >> 24) & 0xFF; + *idx = (_index_t)(wc & 0x7F7F); + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(ei != NULL && wc != NULL); + + if (csid > 7 || (idx & ~0x7F7F) != 0) + return (EINVAL); + + if (csid==0) { + if ((idx & ~0x7F) != 0) + return (EINVAL); + *wc = (wchar_t)idx; + } else { + if ((idx & ~0x7F7F) != 0) + return (EINVAL); + *wc = (wchar_t)idx | ((wchar_t)csid<<24); + } + + return (0); +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(EUCTW); +_CITRUS_CTYPE_DEF_OPS(EUCTW); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(EUCTW); +_CITRUS_STDENC_DEF_OPS(EUCTW); + +#include "citrus_stdenc_template.h" diff --git a/lib/libc/citrus/modules/citrus_euctw.h b/lib/libc/citrus/modules/citrus_euctw.h new file mode 100644 index 0000000000..34b6b36fbc --- /dev/null +++ b/lib/libc/citrus/modules/citrus_euctw.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_euctw.h,v 1.2 2003/06/25 09:51:42 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_euctw.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_EUCTW_H_ +#define _CITRUS_EUCTW_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(EUCTW); +_CITRUS_STDENC_GETOPS_FUNC(EUCTW); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_gbk2k.c b/lib/libc/citrus/modules/citrus_gbk2k.c new file mode 100644 index 0000000000..3d7af8282b --- /dev/null +++ b/lib/libc/citrus/modules/citrus_gbk2k.c @@ -0,0 +1,456 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_gbk2k.c,v 1.4 2003/06/26 12:09:57 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_gbk2k.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_gbk2k.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct _GBK2KState { + char ch[4]; + int chlen; +} _GBK2KState; + +typedef struct { + int ei_mode; +} _GBK2KEncodingInfo; +#define _MODE_2BYTE 0x0001 + +typedef struct { + _GBK2KEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _GBK2KState s_mblen; + _GBK2KState s_mbrlen; + _GBK2KState s_mbrtowc; + _GBK2KState s_mbtowc; + _GBK2KState s_mbsrtowcs; + _GBK2KState s_wcrtomb; + _GBK2KState s_wcsrtombs; + _GBK2KState s_wctomb; + } states; +} _GBK2KCTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_GBK2K_##m +#define _ENCODING_INFO _GBK2KEncodingInfo +#define _CTYPE_INFO _GBK2KCTypeInfo +#define _ENCODING_STATE _GBK2KState +#define _ENCODING_MB_CUR_MAX(_ei_) 4 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + +static __inline void +/*ARGSUSED*/ +_citrus_GBK2K_init_state(_GBK2KEncodingInfo * __restrict ei, + _GBK2KState * __restrict s) +{ + memset(s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_GBK2K_pack_state(_GBK2KEncodingInfo * __restrict ei, + void * __restrict pspriv, + const _GBK2KState * __restrict s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_GBK2K_unpack_state(_GBK2KEncodingInfo * __restrict ei, + _GBK2KState * __restrict s, + const void * __restrict pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static __inline int +_mb_singlebyte(int c) +{ + c &= 0xff; + return (c <= 0x7f); +} + +static __inline int +_mb_leadbyte(int c) +{ + c &= 0xff; + return (0x81 <= c && c <= 0xfe); +} + +static __inline int +_mb_trailbyte(int c) +{ + c &= 0xff; + return ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfe)); +} + +static __inline int +_mb_surrogate(int c) +{ + c &= 0xff; + return (0x30 <= c && c <= 0x39); +} + +static __inline int +_mb_count(wchar_t v) +{ + u_int32_t c; + + c = (u_int32_t)v; /* XXX */ + if (!(c & 0xffffff00)) + return (1); + if (!(c & 0xffff0000)) + return (2); + return (4); +} + +#define _PSENC (psenc->ch[psenc->chlen - 1]) +#define _PUSH_PSENC(c) (psenc->ch[psenc->chlen++] = (c)) + +static int +_citrus_GBK2K_mbrtowc_priv(_GBK2KEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, + const char ** __restrict s, size_t n, + _GBK2KState * __restrict psenc, + size_t * __restrict nresult) +{ + int chlenbak, len; + const char *s0, *s1; + wchar_t wc; + + _DIAGASSERT(ei != NULL); + /* pwc may be NULL */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + s0 = *s; + + if (s0 == NULL) { + /* _citrus_GBK2K_init_state(ei, psenc); */ + psenc->chlen = 0; + *nresult = 0; + return (0); + } + + chlenbak = psenc->chlen; + + switch (psenc->chlen) { + case 3: + if (!_mb_leadbyte (_PSENC)) + goto invalid; + /* FALLTHROUGH */ + case 2: + if (!_mb_surrogate(_PSENC) || _mb_trailbyte(_PSENC)) + goto invalid; + /* FALLTHROUGH */ + case 1: + if (!_mb_leadbyte (_PSENC)) + goto invalid; + /* FALLTHOROUGH */ + case 0: + break; + default: + goto invalid; + } + + for (;;) { + if (n-- < 1) + goto restart; + + _PUSH_PSENC(*s0++); + + switch (psenc->chlen) { + case 1: + if (_mb_singlebyte(_PSENC)) + goto convert; + if (_mb_leadbyte (_PSENC)) + continue; + goto ilseq; + case 2: + if (_mb_trailbyte (_PSENC)) + goto convert; + if ((ei->ei_mode & _MODE_2BYTE) == 0 && + _mb_surrogate (_PSENC)) + continue; + goto ilseq; + case 3: + if (_mb_leadbyte (_PSENC)) + continue; + goto ilseq; + case 4: + if (_mb_surrogate (_PSENC)) + goto convert; + goto ilseq; + } + } + +convert: + len = psenc->chlen; + s1 = &psenc->ch[0]; + wc = 0; + while (len-- > 0) + wc = (wc << 8) | (*s1++ & 0xff); + + if (pwc != NULL) + *pwc = wc; + *s = s0; + *nresult = (wc == 0) ? 0 : psenc->chlen - chlenbak; + /* _citrus_GBK2K_init_state(ei, psenc); */ + psenc->chlen = 0; + + return (0); + +restart: + *s = s0; + *nresult = (size_t)-2; + + return (0); + +invalid: + return (EINVAL); + +ilseq: + *nresult = (size_t)-1; + return (EILSEQ); +} + +static int +_citrus_GBK2K_wcrtomb_priv(_GBK2KEncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _GBK2KState * __restrict psenc, + size_t * __restrict nresult) +{ + int len, ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + if (psenc->chlen != 0) { + ret = EINVAL; + goto err; + } + + len = _mb_count(wc); + if (n < len) { + ret = E2BIG; + goto err; + } + + switch (len) { + case 1: + if (!_mb_singlebyte(_PUSH_PSENC(wc ))) { + ret = EILSEQ; + goto err; + } + break; + case 2: + if (!_mb_leadbyte (_PUSH_PSENC(wc >> 8)) || + !_mb_trailbyte (_PUSH_PSENC(wc ))) { + ret = EILSEQ; + goto err; + } + break; + case 4: + if ((ei->ei_mode & _MODE_2BYTE) != 0 || + !_mb_leadbyte (_PUSH_PSENC(wc >> 24)) || + !_mb_surrogate (_PUSH_PSENC(wc >> 16)) || + !_mb_leadbyte (_PUSH_PSENC(wc >> 8)) || + !_mb_surrogate (_PUSH_PSENC(wc ))) { + ret = EILSEQ; + goto err; + } + break; + } + + _DIAGASSERT(len == psenc->chlen); + + memcpy(s, psenc->ch, psenc->chlen); + *nresult = psenc->chlen; + /* _citrus_GBK2K_init_state(ei, psenc); */ + psenc->chlen = 0; + + return (0); + +err: + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_GBK2K_stdenc_wctocs(_GBK2KEncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + u_int8_t ch, cl; + + _DIAGASSERT(csid != NULL && idx != NULL); + + if ((u_int32_t)wc<0x80) { + /* ISO646 */ + *csid = 0; + *idx = (_index_t)wc; + } else if ((u_int32_t)wc>=0x10000) { + /* GBKUCS : XXX */ + *csid = 3; + *idx = (_index_t)wc; + } else { + ch = (u_int8_t)(wc >> 8); + cl = (u_int8_t)wc; + if (ch>=0xA1 && cl>=0xA1) { + /* EUC G1 */ + *csid = 1; + *idx = (_index_t)wc & 0x7F7FU; + } else { + /* extended area (0x8140-) */ + *csid = 2; + *idx = (_index_t)wc; + } + } + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_GBK2K_stdenc_cstowc(_GBK2KEncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(wc != NULL); + + switch (csid) { + case 0: + /* ISO646 */ + *wc = (wchar_t)idx; + break; + case 1: + /* EUC G1 */ + *wc = (wchar_t)idx | 0x8080U; + break; + case 2: + /* extended area */ + *wc = (wchar_t)idx; + break; + case 3: + /* GBKUCS : XXX */ + if ((ei->ei_mode & _MODE_2BYTE) != 0) + return EINVAL; + *wc = (wchar_t)idx; + break; + default: + return EILSEQ; + } + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_GBK2K_encoding_module_init(_GBK2KEncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + const char *p; + + _DIAGASSERT(ei != NULL); + + p = var; +#define MATCH(x, act) \ +do { \ + if (lenvar >= (sizeof(#x)-1) && \ + _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \ + act; \ + lenvar -= sizeof(#x)-1; \ + p += sizeof(#x)-1; \ + } \ +} while (/*CONSTCOND*/0) + while (lenvar>0) { + switch (_bcs_tolower(*p)) { + case '2': + MATCH("2byte", ei->ei_mode |= _MODE_2BYTE); + break; + } + p++; + lenvar--; + } + + memset((void *)ei, 0, sizeof(*ei)); + return (0); +} + +static void +/*ARGSUSED*/ +_citrus_GBK2K_encoding_module_uninit(_GBK2KEncodingInfo *ei) +{ +} + + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(GBK2K); +_CITRUS_CTYPE_DEF_OPS(GBK2K); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(GBK2K); +_CITRUS_STDENC_DEF_OPS(GBK2K); + +#include "citrus_stdenc_template.h" diff --git a/lib/libc/citrus/modules/citrus_gbk2k.h b/lib/libc/citrus/modules/citrus_gbk2k.h new file mode 100644 index 0000000000..5cdd437090 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_gbk2k.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_gbk2k.h,v 1.2 2003/06/25 09:51:43 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_gbk2k.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_GBK2K_H_ +#define _CITRUS_GBK2K_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(GBK2K); +_CITRUS_STDENC_GETOPS_FUNC(GBK2K); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_iconv_none.c b/lib/libc/citrus/modules/citrus_iconv_none.c new file mode 100644 index 0000000000..f2e1550f00 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_iconv_none.c @@ -0,0 +1,124 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_iconv_none.c,v 1.2 2003/07/01 09:42:16 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_iconv_none.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include + +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_hash.h" +#include "citrus_iconv.h" +#include "citrus_iconv_none.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_ICONV_DECLS(iconv_none); +_CITRUS_ICONV_DEF_OPS(iconv_none); + + +/* ---------------------------------------------------------------------- */ + +int +_citrus_iconv_none_iconv_getops(struct _citrus_iconv_ops *ops, size_t lenops, + uint32_t expected_version) +{ + if (expected_version<_CITRUS_ICONV_ABI_VERSION || lenopsci_closure = NULL; + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_iconv_none_iconv_uninit_shared(struct _citrus_iconv_shared *ci) +{ +} + +static int +/*ARGSUSED*/ +_citrus_iconv_none_iconv_init_context(struct _citrus_iconv *cv) +{ + cv->cv_closure = NULL; +} + +static void +/*ARGSUSED*/ +_citrus_iconv_none_iconv_uninit_context(struct _citrus_iconv *cv) +{ +} + +static int +/*ARGSUSED*/ +_citrus_iconv_none_iconv_convert(struct _citrus_iconv * __restrict ci, + const char * __restrict * __restrict in, + size_t * __restrict inbytes, + char * __restrict * __restrict out, + size_t * __restrict outbytes, + u_int32_t flags, size_t * __restrict invalids) +{ + int e2big; + size_t len; + + len = *inbytes; + e2big = 0; + if (*outbytes +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_region.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_iconv.h" +#include "citrus_stdenc.h" +#include "citrus_mapper.h" +#include "citrus_csmapper.h" +#include "citrus_memstream.h" +#include "citrus_iconv_std.h" +#include "citrus_esdb.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_ICONV_DECLS(iconv_std); +_CITRUS_ICONV_DEF_OPS(iconv_std); + + +/* ---------------------------------------------------------------------- */ + +int +_citrus_iconv_std_iconv_getops(struct _citrus_iconv_ops *ops, size_t lenops, + u_int32_t expected_version) +{ + if (expected_version<_CITRUS_ICONV_ABI_VERSION || lenopsse_ps) + memcpy(se->se_pssaved, se->se_ps, + _stdenc_get_state_size(se->se_handle)); +} + +static __inline void +restore_encoding_state(struct _citrus_iconv_std_encoding *se) +{ + if (se->se_ps) + memcpy(se->se_ps, se->se_pssaved, + _stdenc_get_state_size(se->se_handle)); +} + +static __inline void +init_encoding_state(struct _citrus_iconv_std_encoding *se) +{ + if (se->se_ps) + _stdenc_init_state(se->se_handle, se->se_ps); +} + +static __inline int +mbtocsx(struct _citrus_iconv_std_encoding *se, + _csid_t *csid, _index_t *idx, const char **s, size_t n, + size_t *nresult) +{ + return _stdenc_mbtocs(se->se_handle, csid, idx, s, n, se->se_ps, + nresult); +} + +static __inline int +cstombx(struct _citrus_iconv_std_encoding *se, + char *s, size_t n, _csid_t csid, _index_t idx, size_t *nresult) +{ + return _stdenc_cstomb(se->se_handle, s, n, csid, idx, se->se_ps, + nresult); +} + +static __inline int +wctombx(struct _citrus_iconv_std_encoding *se, + char *s, size_t n, _wc_t wc, size_t *nresult) +{ + return _stdenc_wctomb(se->se_handle, s, n, wc, se->se_ps, nresult); +} + +static __inline int +put_state_resetx(struct _citrus_iconv_std_encoding *se, + char *s, size_t n, size_t *nresult) +{ + return _stdenc_put_state_reset(se->se_handle, s, n, se->se_ps, nresult); +} + +/* + * init encoding context + */ +static int +init_encoding(struct _citrus_iconv_std_encoding *se, struct _stdenc *cs, + void *ps1, void *ps2) +{ + int ret; + + se->se_handle = cs; + se->se_ps = ps1; + se->se_pssaved = ps2; + + if (se->se_ps) + ret = _stdenc_init_state(cs, se->se_ps); + if (!ret && se->se_pssaved) + ret = _stdenc_init_state(cs, se->se_pssaved); + + return ret; +} + +static int +open_csmapper(struct _csmapper **rcm, const char *src, const char *dst, + unsigned long *rnorm) +{ + int ret; + struct _csmapper *cm; + + ret = _csmapper_open(&cm, src, dst, 0, rnorm); + if (ret) + return ret; + if (_csmapper_get_src_max(cm) != 1 || _csmapper_get_dst_max(cm) != 1 || + _csmapper_get_state_size(cm) != 0) { + _csmapper_close(cm); + return EINVAL; + } + + *rcm = cm; + + return 0; +} + +static void +close_dsts(struct _citrus_iconv_std_dst_list *dl) +{ + struct _citrus_iconv_std_dst *sd; + + while ((sd=TAILQ_FIRST(dl)) != NULL) { + TAILQ_REMOVE(dl, sd, sd_entry); + _csmapper_close(sd->sd_mapper); + free(sd); + } +} + +static int +open_dsts(struct _citrus_iconv_std_dst_list *dl, + const struct _esdb_charset *ec, const struct _esdb *dbdst) +{ + int i, ret; + struct _citrus_iconv_std_dst *sd, *sdtmp; + unsigned long norm; + + sd = malloc(sizeof(*sd)); + if (sd == NULL) + return errno; + + for (i=0; idb_num_charsets; i++) { + ret = open_csmapper(&sd->sd_mapper, ec->ec_csname, + dbdst->db_charsets[i].ec_csname, &norm); + if (ret == 0) { + sd->sd_csid = dbdst->db_charsets[i].ec_csid; + sd->sd_norm = norm; + /* insert this mapper by sorted order. */ + TAILQ_FOREACH(sdtmp, dl, sd_entry) { + if (sdtmp->sd_norm > norm) { + TAILQ_INSERT_BEFORE(sdtmp, sd, + sd_entry); + sd = NULL; + break; + } + } + if (sd) + TAILQ_INSERT_TAIL(dl, sd, sd_entry); + sd = malloc(sizeof(*sd)); + if (sd == NULL) { + ret = errno; + close_dsts(dl); + return ret; + } + } else if (ret != ENOENT) { + close_dsts(dl); + free(sd); + return ret; + } + } + free(sd); + return 0; +} + +static void +close_srcs(struct _citrus_iconv_std_src_list *sl) +{ + struct _citrus_iconv_std_src *ss; + + while ((ss=TAILQ_FIRST(sl)) != NULL) { + TAILQ_REMOVE(sl, ss, ss_entry); + close_dsts(&ss->ss_dsts); + free(ss); + } +} + +static int +open_srcs(struct _citrus_iconv_std_src_list *sl, + const struct _esdb *dbsrc, const struct _esdb *dbdst) +{ + int i, ret, count = 0; + struct _citrus_iconv_std_src *ss; + + ss = malloc(sizeof(*ss)); + if (ss == NULL) + return errno; + + TAILQ_INIT(&ss->ss_dsts); + + for (i=0; idb_num_charsets; i++) { + ret = open_dsts(&ss->ss_dsts, &dbsrc->db_charsets[i], dbdst); + if (ret) + goto err; + if (!TAILQ_EMPTY(&ss->ss_dsts)) { + ss->ss_csid = dbsrc->db_charsets[i].ec_csid; + TAILQ_INSERT_TAIL(sl, ss, ss_entry); + ss = malloc(sizeof(*ss)); + if (ss == NULL) { + ret = errno; + goto err; + } + count++; + TAILQ_INIT(&ss->ss_dsts); + } + } + free(ss); + + return count ? 0 : ENOENT; + +err: + free(ss); + close_srcs(sl); + return ret; +} + +/* do convert a character */ +#define E_NO_CORRESPONDING_CHAR ENOENT /* XXX */ +static int +/*ARGSUSED*/ +do_conv(const struct _citrus_iconv_std_shared *is, + struct _citrus_iconv_std_context *sc, _csid_t *csid, _index_t *idx) +{ + _index_t tmpidx; + int ret; + struct _citrus_iconv_std_src *ss; + struct _citrus_iconv_std_dst *sd; + + TAILQ_FOREACH(ss, &is->is_srcs, ss_entry) { + if (ss->ss_csid == *csid) { + TAILQ_FOREACH(sd, &ss->ss_dsts, sd_entry) { + ret = _csmapper_convert(sd->sd_mapper, + &tmpidx, *idx, NULL); + switch (ret) { + case _MAPPER_CONVERT_SUCCESS: + *csid = sd->sd_csid; + *idx = tmpidx; + return 0; + case _MAPPER_CONVERT_NONIDENTICAL: + break; + case _MAPPER_CONVERT_SRC_MORE: + /*FALLTHROUGH*/ + case _MAPPER_CONVERT_DST_MORE: + /*FALLTHROUGH*/ + case _MAPPER_CONVERT_FATAL: + return EINVAL; + case _MAPPER_CONVERT_ILSEQ: + return EILSEQ; + } + } + break; + } + } + + return E_NO_CORRESPONDING_CHAR; +} +/* ---------------------------------------------------------------------- */ + +static int +/*ARGSUSED*/ +_citrus_iconv_std_iconv_init_shared(struct _citrus_iconv_shared *ci, + const char * __restrict curdir, + const char * __restrict src, + const char * __restrict dst, + const void * __restrict var, size_t lenvar) +{ + int ret; + struct _citrus_iconv_std_shared *is; + struct _citrus_esdb esdbsrc, esdbdst; + + is = malloc(sizeof(*is)); + if (is==NULL) { + ret = errno; + goto err0; + } + ret = _citrus_esdb_open(&esdbsrc, src); + if (ret) + goto err1; + ret = _citrus_esdb_open(&esdbdst, dst); + if (ret) + goto err2; + ret = _stdenc_open(&is->is_src_encoding, esdbsrc.db_encname, + esdbsrc.db_variable, esdbsrc.db_len_variable); + if (ret) + goto err3; + ret = _stdenc_open(&is->is_dst_encoding, esdbdst.db_encname, + esdbdst.db_variable, esdbdst.db_len_variable); + if (ret) + goto err4; + is->is_use_invalid = esdbdst.db_use_invalid; + is->is_invalid = esdbdst.db_invalid; + + TAILQ_INIT(&is->is_srcs); + ret = open_srcs(&is->is_srcs, &esdbsrc, &esdbdst); + if (ret) + goto err5; + + _esdb_close(&esdbsrc); + _esdb_close(&esdbdst); + ci->ci_closure = is; + + return 0; + +err5: + _stdenc_close(is->is_dst_encoding); +err4: + _stdenc_close(is->is_src_encoding); +err3: + _esdb_close(&esdbdst); +err2: + _esdb_close(&esdbsrc); +err1: + free(is); +err0: + return ret; +} + +static void +_citrus_iconv_std_iconv_uninit_shared(struct _citrus_iconv_shared *ci) +{ + struct _citrus_iconv_std_shared *is = ci->ci_closure; + + if (is == NULL) + return; + + _stdenc_close(is->is_src_encoding); + _stdenc_close(is->is_dst_encoding); + close_srcs(&is->is_srcs); + free(is); +} + +static int +_citrus_iconv_std_iconv_init_context(struct _citrus_iconv *cv) +{ + const struct _citrus_iconv_std_shared *is = cv->cv_shared->ci_closure; + struct _citrus_iconv_std_context *sc; + int ret; + size_t szpssrc, szpsdst, sz; + char *ptr; + + szpssrc = _stdenc_get_state_size(is->is_src_encoding); + szpsdst = _stdenc_get_state_size(is->is_dst_encoding); + + sz = (szpssrc + szpsdst)*2 + sizeof(struct _citrus_iconv_std_context); + sc = malloc(sz); + if (sc == NULL) + return errno; + + ptr = (char *)&sc[1]; + if (szpssrc) + init_encoding(&sc->sc_src_encoding, is->is_src_encoding, + ptr, ptr+szpssrc); + else + init_encoding(&sc->sc_src_encoding, is->is_src_encoding, + NULL, NULL); + ptr += szpssrc*2; + if (szpsdst) + init_encoding(&sc->sc_dst_encoding, is->is_dst_encoding, + ptr, ptr+szpsdst); + else + init_encoding(&sc->sc_dst_encoding, is->is_dst_encoding, + NULL, NULL); + + cv->cv_closure = (void *)sc; + + return 0; +} + +static void +_citrus_iconv_std_iconv_uninit_context(struct _citrus_iconv *cv) +{ + free(cv->cv_closure); +} + +static int +_citrus_iconv_std_iconv_convert(struct _citrus_iconv * __restrict cv, + const char * __restrict * __restrict in, + size_t * __restrict inbytes, + char * __restrict * __restrict out, + size_t * __restrict outbytes, u_int32_t flags, + size_t * __restrict invalids) +{ + const struct _citrus_iconv_std_shared *is = cv->cv_shared->ci_closure; + struct _citrus_iconv_std_context *sc = cv->cv_closure; + _index_t idx; + _csid_t csid; + int ret; + size_t szrin, szrout; + size_t inval; + const char *tmpin; + + inval = 0; + if (in==NULL || *in==NULL) { + /* special cases */ + if (out!=NULL && *out!=NULL) { + /* init output state and store the shift sequence */ + save_encoding_state(&sc->sc_src_encoding); + save_encoding_state(&sc->sc_dst_encoding); + szrout = 0; + + ret = put_state_resetx(&sc->sc_dst_encoding, + *out, *outbytes, + &szrout); + if (ret) + goto err; + + if (szrout == (size_t)-2) { + /* too small to store the character */ + ret = EINVAL; + goto err; + } + *out += szrout; + *outbytes -= szrout; + } else + /* otherwise, discard the shift sequence */ + init_encoding_state(&sc->sc_dst_encoding); + init_encoding_state(&sc->sc_src_encoding); + *invalids = 0; + return 0; + } + + /* normal case */ + for (;;) { + if (*inbytes==0) + break; + + /* save the encoding states for the error recovery */ + save_encoding_state(&sc->sc_src_encoding); + save_encoding_state(&sc->sc_dst_encoding); + + /* mb -> csid/index */ + tmpin = *in; + szrin = szrout = 0; + ret = mbtocsx(&sc->sc_src_encoding, &csid, &idx, + &tmpin, *inbytes, &szrin); + if (ret) + goto err; + + if (szrin == (size_t)-2) { + /* incompleted character */ + ret = EINVAL; + goto err; + } + /* convert the character */ + ret = do_conv(is, sc, &csid, &idx); + if (ret) { + if (ret == E_NO_CORRESPONDING_CHAR) { + inval++; + szrout = 0; + if ((flags&_CITRUS_ICONV_F_HIDE_INVALID)==0 && + is->is_use_invalid) { + ret = wctombx(&sc->sc_dst_encoding, + *out, *outbytes, + is->is_invalid, + &szrout); + if (ret) + goto err; + } + goto next; + } else { + goto err; + } + } + /* csid/index -> mb */ + ret = cstombx(&sc->sc_dst_encoding, + *out, *outbytes, csid, idx, &szrout); + if (ret) + goto err; +next: + _DIAGASSERT(*inbytes>=szrin && *outbytes>=szrout); + *inbytes -= tmpin-*in; /* szrin is insufficient on \0. */ + *in = tmpin; + *outbytes -= szrout; + *out += szrout; + } + *invalids = inval; + + return 0; + +err: + restore_encoding_state(&sc->sc_src_encoding); + restore_encoding_state(&sc->sc_dst_encoding); +err_norestore: + *invalids = inval; + + return ret; +} diff --git a/lib/libc/citrus/modules/citrus_iconv_std.h b/lib/libc/citrus/modules/citrus_iconv_std.h new file mode 100644 index 0000000000..7db4bd66aa --- /dev/null +++ b/lib/libc/citrus/modules/citrus_iconv_std.h @@ -0,0 +1,37 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_iconv_std.h,v 1.1 2003/06/25 09:51:44 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_iconv_std.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_ICONV_STD_H_ +#define _CITRUS_ICONV_STD_H_ + +#include "citrus_iconv_std_local.h" + +_CITRUS_ICONV_GETOPS_FUNC(iconv_std); + +#endif diff --git a/lib/libc/citrus/modules/citrus_iconv_std_local.h b/lib/libc/citrus/modules/citrus_iconv_std_local.h new file mode 100644 index 0000000000..e86ea9e17d --- /dev/null +++ b/lib/libc/citrus/modules/citrus_iconv_std_local.h @@ -0,0 +1,82 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_iconv_std_local.h,v 1.2 2003/07/01 09:42:16 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_iconv_std_local.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_ICONV_STD_LOCAL_H_ +#define _CITRUS_ICONV_STD_LOCAL_H_ + +/* + * encoding + */ +struct _citrus_iconv_std_encoding { + struct _citrus_stdenc *se_handle; + void *se_ps; + void *se_pssaved; +}; + +/* + * dst + */ +struct _citrus_iconv_std_dst { + TAILQ_ENTRY(_citrus_iconv_std_dst) sd_entry; + _citrus_csid_t sd_csid; + unsigned long sd_norm; + struct _citrus_csmapper *sd_mapper; +}; +TAILQ_HEAD(_citrus_iconv_std_dst_list, _citrus_iconv_std_dst); + +/* + * src + */ +struct _citrus_iconv_std_src { + TAILQ_ENTRY(_citrus_iconv_std_src) ss_entry; + _citrus_csid_t ss_csid; + struct _citrus_iconv_std_dst_list ss_dsts; +}; +TAILQ_HEAD(_citrus_iconv_std_src_list, _citrus_iconv_std_src); + +/* + * iconv_std handle + */ +struct _citrus_iconv_std_shared { + struct _citrus_stdenc *is_src_encoding; + struct _citrus_stdenc *is_dst_encoding; + struct _citrus_iconv_std_src_list is_srcs; + int is_use_invalid; + _citrus_wc_t is_invalid; +}; + +/* + * iconv_std context + */ +struct _citrus_iconv_std_context { + struct _citrus_iconv_std_encoding sc_src_encoding; + struct _citrus_iconv_std_encoding sc_dst_encoding; +}; + +#endif diff --git a/lib/libc/citrus/modules/citrus_iso2022.c b/lib/libc/citrus/modules/citrus_iso2022.c new file mode 100644 index 0000000000..0bf2438384 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_iso2022.c @@ -0,0 +1,1299 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_iso2022.c,v 1.13 2005/02/10 19:03:51 tnozaki Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_iso2022.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)1999, 2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/iso2022.c,v 1.23 2001/06/21 01:51:44 yamt Exp $ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_iso2022.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + + +/* + * wchar_t mappings: + * ASCII (ESC ( B) 00000000 00000000 00000000 0xxxxxxx + * iso-8859-1 (ESC , A) 00000000 00000000 00000000 1xxxxxxx + * 94 charset (ESC ( F) 0fffffff 00000000 00000000 0xxxxxxx + * 94 charset (ESC ( M F) 0fffffff 1mmmmmmm 00000000 0xxxxxxx + * 96 charset (ESC , F) 0fffffff 00000000 00000000 1xxxxxxx + * 96 charset (ESC , M F) 0fffffff 1mmmmmmm 00000000 1xxxxxxx + * 94x94 charset (ESC $ ( F) 0fffffff 00000000 0xxxxxxx 0xxxxxxx + * 96x96 charset (ESC $ , F) 0fffffff 00000000 0xxxxxxx 1xxxxxxx + * 94x94 charset (ESC & V ESC $ ( F) + * 0fffffff 1vvvvvvv 0xxxxxxx 0xxxxxxx + * 94x94x94 charset (ESC $ ( F) 0fffffff 0xxxxxxx 0xxxxxxx 0xxxxxxx + * 96x96x96 charset (ESC $ , F) 0fffffff 0xxxxxxx 0xxxxxxx 1xxxxxxx + * reserved for UCS4 co-existence (UCS4 is 31bit encoding thanks to mohta bit) + * 1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx + */ + +typedef struct { + u_char type; +#define CS94 (0U) +#define CS96 (1U) +#define CS94MULTI (2U) +#define CS96MULTI (3U) + + u_char final; + u_char interm; + u_char vers; +} _ISO2022Charset; + +typedef struct { + _ISO2022Charset g[4]; + /* need 3 bits to hold -1, 0, ..., 3 */ + int gl:3, + gr:3, + singlegl:3, + singlegr:3; + char ch[7]; /* longest escape sequence (ESC & V ESC $ ( F) */ + int chlen; + int flags; +#define _ISO2022STATE_FLAG_INITIALIZED 1 +} _ISO2022State; + +typedef struct { + _ISO2022Charset *recommend[4]; + size_t recommendsize[4]; + _ISO2022Charset initg[4]; + int maxcharset; + int flags; +#define F_8BIT 0x0001 +#define F_NOOLD 0x0002 +#define F_SI 0x0010 /*0F*/ +#define F_SO 0x0020 /*0E*/ +#define F_LS0 0x0010 /*0F*/ +#define F_LS1 0x0020 /*0E*/ +#define F_LS2 0x0040 /*ESC n*/ +#define F_LS3 0x0080 /*ESC o*/ +#define F_LS1R 0x0100 /*ESC ~*/ +#define F_LS2R 0x0200 /*ESC }*/ +#define F_LS3R 0x0400 /*ESC |*/ +#define F_SS2 0x0800 /*ESC N*/ +#define F_SS3 0x1000 /*ESC O*/ +#define F_SS2R 0x2000 /*8E*/ +#define F_SS3R 0x4000 /*8F*/ +} _ISO2022EncodingInfo; +typedef struct { + _ISO2022EncodingInfo ei; + struct { + /* for future multi-locale facility */ + _ISO2022State s_mblen; + _ISO2022State s_mbrlen; + _ISO2022State s_mbrtowc; + _ISO2022State s_mbtowc; + _ISO2022State s_mbsrtowcs; + _ISO2022State s_wcrtomb; + _ISO2022State s_wcsrtombs; + _ISO2022State s_wctomb; + } states; +} _ISO2022CTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_ISO2022_##m +#define _ENCODING_INFO _ISO2022EncodingInfo +#define _CTYPE_INFO _ISO2022CTypeInfo +#define _ENCODING_STATE _ISO2022State +#define _ENCODING_MB_CUR_MAX(_ei_) MB_LEN_MAX +#define _ENCODING_IS_STATE_DEPENDENT 1 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) \ + (!((_ps_)->flags & _ISO2022STATE_FLAG_INITIALIZED)) + + +#define _ISO2022INVALID (wchar_t)-1 + +static __inline int isc0(__uint8_t x) { return ((x & 0x1f) == x); } +static __inline int isc1(__uint8_t x) { return (0x80 <= x && x <= 0x9f); } +static __inline int iscntl(__uint8_t x) { return (isc0(x) || isc1(x) || x == 0x7f); } +static __inline int is94(__uint8_t x) { return (0x21 <= x && x <= 0x7e); } +static __inline int is96(__uint8_t x) { return (0x20 <= x && x <= 0x7f); } +static __inline int isecma(__uint8_t x) { return (0x30 <= x && x <= 0x7f); } +static __inline int isinterm(__uint8_t x) { return (0x20 <= x && x <= 0x2f); } +static __inline int isthree(__uint8_t x) { return (0x60 <= x && x <= 0x6f); } + +static __inline int +getcs(const char * __restrict p, _ISO2022Charset * __restrict cs) +{ + + _DIAGASSERT(p != NULL); + _DIAGASSERT(cs != NULL); + + if (!strncmp(p, "94$", 3) && p[3] && !p[4]) { + cs->final = (u_char)(p[3] & 0xff); + cs->interm = '\0'; + cs->vers = '\0'; + cs->type = CS94MULTI; + } else if (!strncmp(p, "96$", 3) && p[3] && !p[4]) { + cs->final = (u_char)(p[3] & 0xff); + cs->interm = '\0'; + cs->vers = '\0'; + cs->type = CS96MULTI; + } else if (!strncmp(p, "94", 2) && p[2] && !p[3]) { + cs->final = (u_char)(p[2] & 0xff); + cs->interm = '\0'; + cs->vers = '\0'; + cs->type = CS94; + } else if (!strncmp(p, "96", 2) && p[2] && !p[3]) { + cs->final = (u_char )(p[2] & 0xff); + cs->interm = '\0'; + cs->vers = '\0'; + cs->type = CS96; + } else { + return 1; + } + + return 0; +} + + +#define _NOTMATCH 0 +#define _MATCH 1 +#define _PARSEFAIL 2 + +static __inline int +get_recommend(_ISO2022EncodingInfo * __restrict ei, + const char * __restrict token) +{ + int i; + _ISO2022Charset cs, *p; + + if (!strchr("0123", token[0]) || token[1] != '=') + return (_NOTMATCH); + + if (getcs(&token[2], &cs) == 0) + ; + else if (!strcmp(&token[2], "94")) { + cs.final = (u_char)(token[4]); + cs.interm = '\0'; + cs.vers = '\0'; + cs.type = CS94; + } else if (!strcmp(&token[2], "96")) { + cs.final = (u_char)(token[4]); + cs.interm = '\0'; + cs.vers = '\0'; + cs.type = CS96; + } else if (!strcmp(&token[2], "94$")) { + cs.final = (u_char)(token[5]); + cs.interm = '\0'; + cs.vers = '\0'; + cs.type = CS94MULTI; + } else if (!strcmp(&token[2], "96$")) { + cs.final = (u_char)(token[5]); + cs.interm = '\0'; + cs.vers = '\0'; + cs.type = CS96MULTI; + } else { + return (_PARSEFAIL); + } + + i = token[0] - '0'; + if (!ei->recommend[i]) { + ei->recommend[i] = malloc(sizeof(_ISO2022Charset)); + } else { + p = realloc(ei->recommend[i], + sizeof(_ISO2022Charset) * (ei->recommendsize[i] + 1)); + if (!p) + return (_PARSEFAIL); + ei->recommend[i] = p; + } + if (!ei->recommend[i]) + return (_PARSEFAIL); + ei->recommendsize[i]++; + + (ei->recommend[i] + (ei->recommendsize[i] - 1))->final = cs.final; + (ei->recommend[i] + (ei->recommendsize[i] - 1))->interm = cs.interm; + (ei->recommend[i] + (ei->recommendsize[i] - 1))->vers = cs.vers; + (ei->recommend[i] + (ei->recommendsize[i] - 1))->type = cs.type; + + return (_MATCH); +} + +static __inline int +get_initg(_ISO2022EncodingInfo * __restrict ei, + const char * __restrict token) +{ + _ISO2022Charset cs; + + if (strncmp("INIT", &token[0], 4) || + !strchr("0123", token[4]) || + token[5] != '=') + return (_NOTMATCH); + + if (getcs(&token[6], &cs) != 0) + return (_PARSEFAIL); + + ei->initg[token[4] - '0'].type = cs.type; + ei->initg[token[4] - '0'].final = cs.final; + ei->initg[token[4] - '0'].interm = cs.interm; + ei->initg[token[4] - '0'].vers = cs.vers; + + return (_MATCH); +} + +static __inline int +get_max(_ISO2022EncodingInfo * __restrict ei, + const char * __restrict token) +{ + if (!strcmp(token, "MAX1")) { + ei->maxcharset = 1; + } else if (!strcmp(token, "MAX2")) { + ei->maxcharset = 2; + } else if (!strcmp(token, "MAX3")) { + ei->maxcharset = 3; + } else + return (_NOTMATCH); + + return (_MATCH); +} + + +static __inline int +get_flags(_ISO2022EncodingInfo * __restrict ei, + const char * __restrict token) +{ + int i; + static struct { + const char *tag; + int flag; + } const tags[] = { + { "DUMMY", 0 }, + { "8BIT", F_8BIT }, + { "NOOLD", F_NOOLD }, + { "SI", F_SI }, + { "SO", F_SO }, + { "LS0", F_LS0 }, + { "LS1", F_LS1 }, + { "LS2", F_LS2 }, + { "LS3", F_LS3 }, + { "LS1R", F_LS1R }, + { "LS2R", F_LS2R }, + { "LS3R", F_LS3R }, + { "SS2", F_SS2 }, + { "SS3", F_SS3 }, + { "SS2R", F_SS2R }, + { "SS3R", F_SS3R }, + { NULL, 0 } + }; + + for (i = 0; tags[i].tag; i++) { + if (!strcmp(token, tags[i].tag)) { + ei->flags |= tags[i].flag; + return (_MATCH); + } + } + + return (_NOTMATCH); +} + + +static __inline int +_citrus_ISO2022_parse_variable(_ISO2022EncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + char const *v, *e; + char buf[20]; + int i, len, ret; + + _DIAGASSERT(ei != NULL); + + + /* + * parse VARIABLE section. + */ + + if (!var) + return (EFTYPE); + + v = (const char *) var; + + /* initialize structure */ + ei->maxcharset = 0; + for (i = 0; i < 4; i++) { + ei->recommend[i] = NULL; + ei->recommendsize[i] = 0; + } + ei->flags = 0; + + while (*v) { + while (*v == ' ' || *v == '\t') + ++v; + + /* find the token */ + e = v; + while (*e && *e != ' ' && *e != '\t') + ++e; + + len = e-v; + if (len == 0) + break; + if (len>=sizeof(buf)) + goto parsefail; + snprintf(buf, sizeof(buf), "%.*s", len, v); + + if ((ret = get_recommend(ei, buf)) != _NOTMATCH) + ; + else if ((ret = get_initg(ei, buf)) != _NOTMATCH) + ; + else if ((ret = get_max(ei, buf)) != _NOTMATCH) + ; + else if ((ret = get_flags(ei, buf)) != _NOTMATCH) + ; + else + ret = _PARSEFAIL; + if (ret==_PARSEFAIL) + goto parsefail; + v = e; + + } + + return (0); + +parsefail: + free(ei->recommend[0]); + free(ei->recommend[1]); + free(ei->recommend[2]); + free(ei->recommend[3]); + + return (EFTYPE); +} + +static __inline void +/*ARGSUSED*/ +_citrus_ISO2022_init_state(_ISO2022EncodingInfo * __restrict ei, + _ISO2022State * __restrict s) +{ + int i; + + memset(s, 0, sizeof(*s)); + s->gl = 0; + s->gr = (ei->flags & F_8BIT) ? 1 : -1; + + for (i = 0; i < 4; i++) { + if (ei->initg[i].final) { + s->g[i].type = ei->initg[i].type; + s->g[i].final = ei->initg[i].final; + s->g[i].interm = ei->initg[i].interm; + } + } + s->singlegl = s->singlegr = -1; + s->flags |= _ISO2022STATE_FLAG_INITIALIZED; +} + +static __inline void +/*ARGSUSED*/ +_citrus_ISO2022_pack_state(_ISO2022EncodingInfo * __restrict ei, + void * __restrict pspriv, + const _ISO2022State * __restrict s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_ISO2022_unpack_state(_ISO2022EncodingInfo * __restrict ei, + _ISO2022State * __restrict s, + const void * __restrict pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static int +/*ARGSUSED*/ +_citrus_ISO2022_encoding_module_init(_ISO2022EncodingInfo * __restrict ei, + const void * __restrict var, + size_t lenvar) +{ + + _DIAGASSERT(ei != NULL); + + return _citrus_ISO2022_parse_variable(ei, var, lenvar); +} + +static void +/*ARGSUSED*/ +_citrus_ISO2022_encoding_module_uninit(_ISO2022EncodingInfo *ei) +{ +} + +#define ESC '\033' +#define ECMA -1 +#define INTERM -2 +#define OECMA -3 + +static const struct seqtable { + int type; + int csoff; + int finaloff; + int intermoff; + int versoff; + int len; + int chars[10]; +} seqtable[] = { + /* G0 94MULTI special */ + { CS94MULTI, -1, 2, -1, -1, 3, { ESC, '$', OECMA }, }, + /* G0 94MULTI special with version identification */ + { CS94MULTI, -1, 5, -1, 2, 6, { ESC, '&', ECMA, ESC, '$', OECMA }, }, + /* G? 94 */ + { CS94, 1, 2, -1, -1, 3, { ESC, CS94, ECMA, }, }, + /* G? 94 with 2nd intermediate char */ + { CS94, 1, 3, 2, -1, 4, { ESC, CS94, INTERM, ECMA, }, }, + /* G? 96 */ + { CS96, 1, 2, -1, -1, 3, { ESC, CS96, ECMA, }, }, + /* G? 96 with 2nd intermediate char */ + { CS96, 1, 3, 2, -1, 4, { ESC, CS96, INTERM, ECMA, }, }, + /* G? 94MULTI */ + { CS94MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS94, ECMA, }, }, + /* G? 96MULTI */ + { CS96MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS96, ECMA, }, }, + /* G? 94MULTI with version specification */ + { CS94MULTI, 5, 6, -1, 2, 7, { ESC, '&', ECMA, ESC, '$', CS94, ECMA, }, }, + /* LS2/3 */ + { -1, -1, -1, -1, -1, 2, { ESC, 'n', }, }, + { -1, -1, -1, -1, -1, 2, { ESC, 'o', }, }, + /* LS1/2/3R */ + { -1, -1, -1, -1, -1, 2, { ESC, '~', }, }, + { -1, -1, -1, -1, -1, 2, { ESC, /*{*/ '}', }, }, + { -1, -1, -1, -1, -1, 2, { ESC, '|', }, }, + /* SS2/3 */ + { -1, -1, -1, -1, -1, 2, { ESC, 'N', }, }, + { -1, -1, -1, -1, -1, 2, { ESC, 'O', }, }, + /* end of records */ + { 0, } +}; + +static int +seqmatch(const char * __restrict s, size_t n, + const struct seqtable * __restrict sp) +{ + const int *p; + + _DIAGASSERT(s != NULL); + _DIAGASSERT(sp != NULL); + + p = sp->chars; + while (p - sp->chars < n && p - sp->chars < sp->len) { + switch (*p) { + case ECMA: + if (!isecma(*s)) + goto terminate; + break; + case OECMA: + if (*s && strchr("@AB", *s)) + break; + else + goto terminate; + case INTERM: + if (!isinterm(*s)) + goto terminate; + break; + case CS94: + if (*s && strchr("()*+", *s)) + break; + else + goto terminate; + case CS96: + if (*s && strchr(",-./", *s)) + break; + else + goto terminate; + default: + if (*s != *p) + goto terminate; + break; + } + + p++; + s++; + } + +terminate: + return p - sp->chars; +} + +static wchar_t +_ISO2022_sgetwchar(_ISO2022EncodingInfo * __restrict ei, + const char * __restrict string, size_t n, + const char ** __restrict result, + _ISO2022State * __restrict psenc) +{ + wchar_t wchar = 0; + int cur; + const struct seqtable *sp; + int nmatch; + int i; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(string != NULL); + /* result may be NULL */ + + while (1) { + /* SI/SO */ + if (1 <= n && string[0] == '\017') { + psenc->gl = 0; + string++; + n--; + continue; + } + if (1 <= n && string[0] == '\016') { + psenc->gl = 1; + string++; + n--; + continue; + } + + /* SS2/3R */ + if (1 <= n && string[0] && strchr("\217\216", string[0])) { + psenc->singlegl = psenc->singlegr = + (string[0] - '\216') + 2; + string++; + n--; + continue; + } + + /* eat the letter if this is not ESC */ + if (1 <= n && string[0] != '\033') + break; + + /* look for a perfect match from escape sequences */ + for (sp = &seqtable[0]; sp->len; sp++) { + nmatch = seqmatch(string, n, sp); + if (sp->len == nmatch && n >= sp->len) + break; + } + + if (!sp->len) + goto notseq; + + if (sp->type != -1) { + if (sp->csoff == -1) + i = 0; + else { + switch (sp->type) { + case CS94: + case CS94MULTI: + i = string[sp->csoff] - '('; + break; + case CS96: + case CS96MULTI: + i = string[sp->csoff] - ','; + break; + } + } + psenc->g[i].type = sp->type; + psenc->g[i].final = '\0'; + psenc->g[i].interm = '\0'; + psenc->g[i].vers = '\0'; + /* sp->finaloff must not be -1 */ + if (sp->finaloff != -1) + psenc->g[i].final = string[sp->finaloff]; + if (sp->intermoff != -1) + psenc->g[i].interm = string[sp->intermoff]; + if (sp->versoff != -1) + psenc->g[i].vers = string[sp->versoff]; + + string += sp->len; + n -= sp->len; + continue; + } + + /* LS2/3 */ + if (2 <= n && string[0] == '\033' + && string[1] && strchr("no", string[1])) { + psenc->gl = string[1] - 'n' + 2; + string += 2; + n -= 2; + continue; + } + + /* LS1/2/3R */ + /* XXX: { for vi showmatch */ + if (2 <= n && string[0] == '\033' + && string[1] && strchr("~}|", string[1])) { + psenc->gr = 3 - (string[1] - '|'); + string += 2; + n -= 2; + continue; + } + + /* SS2/3 */ + if (2 <= n && string[0] == '\033' + && string[1] && strchr("NO", string[1])) { + psenc->singlegl = (string[1] - 'N') + 2; + string += 2; + n -= 2; + continue; + } + + notseq: + /* + * if we've got an unknown escape sequence, eat the ESC at the + * head. otherwise, wait till full escape sequence comes. + */ + for (sp = &seqtable[0]; sp->len; sp++) { + nmatch = seqmatch(string, n, sp); + if (!nmatch) + continue; + + /* + * if we are in the middle of escape sequence, + * we still need to wait for more characters to come + */ + if (n < sp->len) { + if (nmatch == n) { + if (result) + *result = string; + return (_ISO2022INVALID); + } + } else { + if (nmatch == sp->len) { + /* this case should not happen */ + goto eat; + } + } + } + + break; + } + +eat: + /* no letter to eat */ + if (n < 1) { + if (result) + *result = string; + return (_ISO2022INVALID); + } + + /* normal chars. always eat C0/C1 as is. */ + if (iscntl(*string & 0xff)) + cur = -1; + else if (*string & 0x80) { + cur = (psenc->singlegr == -1) + ? psenc->gr : psenc->singlegr; + } else { + cur = (psenc->singlegl == -1) + ? psenc->gl : psenc->singlegl; + } + + if (cur == -1) { +asis: + wchar = *string++ & 0xff; + if (result) + *result = string; + /* reset single shift state */ + psenc->singlegr = psenc->singlegl = -1; + return wchar; + } + + /* length error check */ + switch (psenc->g[cur].type) { + case CS94MULTI: + case CS96MULTI: + if (!isthree(psenc->g[cur].final)) { + if (2 <= n + && (string[0] & 0x80) == (string[1] & 0x80)) + break; + } else { + if (3 <= n + && (string[0] & 0x80) == (string[1] & 0x80) + && (string[0] & 0x80) == (string[2] & 0x80)) + break; + } + + /* we still need to wait for more characters to come */ + if (result) + *result = string; + return (_ISO2022INVALID); + + case CS94: + case CS96: + if (1 <= n) + break; + + /* we still need to wait for more characters to come */ + if (result) + *result = string; + return (_ISO2022INVALID); + } + + /* range check */ + switch (psenc->g[cur].type) { + case CS94: + if (!(is94(string[0] & 0x7f))) + goto asis; + case CS96: + if (!(is96(string[0] & 0x7f))) + goto asis; + break; + case CS94MULTI: + if (!(is94(string[0] & 0x7f) && is94(string[1] & 0x7f))) + goto asis; + break; + case CS96MULTI: + if (!(is96(string[0] & 0x7f) && is96(string[1] & 0x7f))) + goto asis; + break; + } + + /* extract the character. */ + switch (psenc->g[cur].type) { + case CS94: + /* special case for ASCII. */ + if (psenc->g[cur].final == 'B' && !psenc->g[cur].interm) { + wchar = *string++; + wchar &= 0x7f; + break; + } + wchar = psenc->g[cur].final; + wchar = (wchar << 8); + wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0); + wchar = (wchar << 8); + wchar = (wchar << 8) | (*string++ & 0x7f); + break; + case CS96: + /* special case for ISO-8859-1. */ + if (psenc->g[cur].final == 'A' && !psenc->g[cur].interm) { + wchar = *string++; + wchar &= 0x7f; + wchar |= 0x80; + break; + } + wchar = psenc->g[cur].final; + wchar = (wchar << 8); + wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0); + wchar = (wchar << 8); + wchar = (wchar << 8) | (*string++ & 0x7f); + wchar |= 0x80; + break; + case CS94MULTI: + case CS96MULTI: + wchar = psenc->g[cur].final; + wchar = (wchar << 8); + if (isthree(psenc->g[cur].final)) + wchar |= (*string++ & 0x7f); + wchar = (wchar << 8) | (*string++ & 0x7f); + wchar = (wchar << 8) | (*string++ & 0x7f); + if (psenc->g[cur].type == CS96MULTI) + wchar |= 0x80; + break; + } + + if (result) + *result = string; + /* reset single shift state */ + psenc->singlegr = psenc->singlegl = -1; + return wchar; +} + + + +static int +_citrus_ISO2022_mbrtowc_priv(_ISO2022EncodingInfo * __restrict ei, + wchar_t * __restrict pwc, + const char ** __restrict s, + size_t n, _ISO2022State * __restrict psenc, + size_t * __restrict nresult) +{ + wchar_t wchar; + const char *s0, *p, *result; + int c; + int chlenbak; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s != NULL); + + s0 = *s; + c = 0; + chlenbak = psenc->chlen; + + /* + * if we have something in buffer, use that. + * otherwise, skip here + */ + if (psenc->chlen < 0 || psenc->chlen > sizeof(psenc->ch)) { + /* illgeal state */ + _citrus_ISO2022_init_state(ei, psenc); + goto encoding_error; + } + if (psenc->chlen == 0) + goto emptybuf; + + /* buffer is not empty */ + p = psenc->ch; + while (psenc->chlen < sizeof(psenc->ch) && n >= 0) { + if (n > 0) { + psenc->ch[psenc->chlen++] = *s0++; + n--; + } + + wchar = _ISO2022_sgetwchar(ei, p, psenc->chlen - (p-psenc->ch), + &result, psenc); + if (wchar != _ISO2022INVALID) { + c += result - p; + if (psenc->chlen > c) + memmove(psenc->ch, result, psenc->chlen - c); + if (psenc->chlen < c) + psenc->chlen = 0; + else + psenc->chlen -= c; + goto output; + } + + c += result - p; + p = result; + + if (n == 0) + goto restart; + } + + /* escape sequence too long? */ + goto encoding_error; + +emptybuf: + wchar = _ISO2022_sgetwchar(ei, s0, n, &result, psenc); + if (wchar != _ISO2022INVALID) { + c += result - s0; + psenc->chlen = 0; + s0 = result; + goto output; + } + if (result > s0 && n > result - s0) { + c += (result - s0); + n -= (result - s0); + s0 = result; + goto emptybuf; + } + n += c; + if (n < sizeof(psenc->ch)) { + memcpy(psenc->ch, s0 - c, n); + psenc->chlen = n; + s0 = result; + goto restart; + } + + /* escape sequence too long? */ + +encoding_error: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +output: + *s = s0; + if (pwc) + *pwc = wchar; + + if (!wchar) + *nresult = 0; + else + *nresult = c - chlenbak; + + return (0); + +restart: + *s = s0; + *nresult = (size_t)-2; + + return (0); +} + +static int +recommendation(_ISO2022EncodingInfo * __restrict ei, + _ISO2022Charset * __restrict cs) +{ + int i, j; + _ISO2022Charset *recommend; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(cs != NULL); + + /* first, try a exact match. */ + for (i = 0; i < 4; i++) { + recommend = ei->recommend[i]; + for (j = 0; j < ei->recommendsize[i]; j++) { + if (cs->type != recommend[j].type) + continue; + if (cs->final != recommend[j].final) + continue; + if (cs->interm != recommend[j].interm) + continue; + + return i; + } + } + + /* then, try a wildcard match over final char. */ + for (i = 0; i < 4; i++) { + recommend = ei->recommend[i]; + for (j = 0; j < ei->recommendsize[i]; j++) { + if (cs->type != recommend[j].type) + continue; + if (cs->final && (cs->final != recommend[j].final)) + continue; + if (cs->interm && (cs->interm != recommend[j].interm)) + continue; + + return i; + } + } + + /* there's no recommendation. make a guess. */ + if (ei->maxcharset == 0) { + return 0; + } else { + switch (cs->type) { + case CS94: + case CS94MULTI: + return 0; + case CS96: + case CS96MULTI: + return 1; + } + } + return 0; +} + +static int +_ISO2022_sputwchar(_ISO2022EncodingInfo * __restrict ei, wchar_t wc, + char * __restrict string, size_t n, + char ** __restrict result, + _ISO2022State * __restrict psenc) +{ + int i = 0, len; + _ISO2022Charset cs; + char *p; + char tmp[MB_LEN_MAX]; + int target; + u_char mask; + int bit8; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(string != NULL); + /* result may be NULL */ + /* state appears to be unused */ + + if (iscntl(wc & 0xff)) { + /* go back to ASCII on control chars */ + cs.type = CS94; + cs.final = 'B'; + cs.interm = '\0'; + } else if (!(wc & ~0xff)) { + if (wc & 0x80) { + /* special treatment for ISO-8859-1 */ + cs.type = CS96; + cs.final = 'A'; + cs.interm = '\0'; + } else { + /* special treatment for ASCII */ + cs.type = CS94; + cs.final = 'B'; + cs.interm = '\0'; + } + } else { + cs.final = (wc >> 24) & 0x7f; + if ((wc >> 16) & 0x80) + cs.interm = (wc >> 16) & 0x7f; + else + cs.interm = '\0'; + if (wc & 0x80) + cs.type = (wc & 0x00007f00) ? CS96MULTI : CS96; + else + cs.type = (wc & 0x00007f00) ? CS94MULTI : CS94; + } + target = recommendation(ei, &cs); + p = tmp; + bit8 = ei->flags & F_8BIT; + + /* designate the charset onto the target plane(G0/1/2/3). */ + if (psenc->g[target].type == cs.type + && psenc->g[target].final == cs.final + && psenc->g[target].interm == cs.interm) + goto planeok; + + *p++ = '\033'; + if (cs.type == CS94MULTI || cs.type == CS96MULTI) + *p++ = '$'; + if (target == 0 && cs.type == CS94MULTI && strchr("@AB", cs.final) + && !cs.interm && !(ei->flags & F_NOOLD)) + ; + else if (cs.type == CS94 || cs.type == CS94MULTI) + *p++ = "()*+"[target]; + else + *p++ = ",-./"[target]; + if (cs.interm) + *p++ = cs.interm; + *p++ = cs.final; + + psenc->g[target].type = cs.type; + psenc->g[target].final = cs.final; + psenc->g[target].interm = cs.interm; + +planeok: + /* invoke the plane onto GL or GR. */ + if (psenc->gl == target) + goto sideok; + if (bit8 && psenc->gr == target) + goto sideok; + + if (target == 0 && (ei->flags & F_LS0)) { + *p++ = '\017'; + psenc->gl = 0; + } else if (target == 1 && (ei->flags & F_LS1)) { + *p++ = '\016'; + psenc->gl = 1; + } else if (target == 2 && (ei->flags & F_LS2)) { + *p++ = '\033'; + *p++ = 'n'; + psenc->gl = 2; + } else if (target == 3 && (ei->flags & F_LS3)) { + *p++ = '\033'; + *p++ = 'o'; + psenc->gl = 3; + } else if (bit8 && target == 1 && (ei->flags & F_LS1R)) { + *p++ = '\033'; + *p++ = '~'; + psenc->gr = 1; + } else if (bit8 && target == 2 && (ei->flags & F_LS2R)) { + *p++ = '\033'; + /*{*/ + *p++ = '}'; + psenc->gr = 2; + } else if (bit8 && target == 3 && (ei->flags & F_LS3R)) { + *p++ = '\033'; + *p++ = '|'; + psenc->gr = 3; + } else if (target == 2 && (ei->flags & F_SS2)) { + *p++ = '\033'; + *p++ = 'N'; + psenc->singlegl = 2; + } else if (target == 3 && (ei->flags & F_SS3)) { + *p++ = '\033'; + *p++ = 'O'; + psenc->singlegl = 3; + } else if (bit8 && target == 2 && (ei->flags & F_SS2R)) { + *p++ = '\216'; + *p++ = 'N'; + psenc->singlegl = psenc->singlegr = 2; + } else if (bit8 && target == 3 && (ei->flags & F_SS3R)) { + *p++ = '\217'; + *p++ = 'O'; + psenc->singlegl = psenc->singlegr = 3; + } else + abort(); + +sideok: + if (psenc->singlegl == target) + mask = 0x00; + else if (psenc->singlegr == target) + mask = 0x80; + else if (psenc->gl == target) + mask = 0x00; + else if ((ei->flags & F_8BIT) && psenc->gr == target) + mask = 0x80; + else + abort(); + + switch (cs.type) { + case CS94: + case CS96: + i = 1; + break; + case CS94MULTI: + case CS96MULTI: + i = isthree(cs.final) ? 3 : 2; + break; + } + while (i-- > 0) + *p++ = ((wc >> (i << 3)) & 0x7f) | mask; + + /* reset single shift state */ + psenc->singlegl = psenc->singlegr = -1; + + len = p - tmp; + if (n < len) { + if (result) + *result = (char *)0; + } else { + if (result) + *result = string + len; + memcpy(string, tmp, len); + } + return len; +} + +static int +_citrus_ISO2022_put_state_reset(_ISO2022EncodingInfo * __restrict ei, + char * __restrict s, size_t n, + _ISO2022State * __restrict psenc, + size_t * __restrict nresult) +{ + char buf[MB_LEN_MAX]; + char *result; + int len, ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + /* XXX state will be modified after this operation... */ + len = _ISO2022_sputwchar(ei, L'\0', buf, sizeof(buf), &result, psenc); + if (len==0) { + ret = EINVAL; + goto err; + } + if (sizeof(buf) < len || n < len-1) { + /* XXX should recover state? */ + ret = E2BIG; + goto err; + } + + memcpy(s, buf, len-1); + *nresult = (size_t)(len-1); + return (0); + +err: + /* bound check failure */ + *nresult = (size_t)-1; + return ret; +} + +static int +_citrus_ISO2022_wcrtomb_priv(_ISO2022EncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _ISO2022State * __restrict psenc, + size_t * __restrict nresult) +{ + char buf[MB_LEN_MAX]; + char *result; + int len, ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + /* XXX state will be modified after this operation... */ + len = _ISO2022_sputwchar(ei, wc, buf, sizeof(buf), &result, psenc); + if (sizeof(buf) < len || n < len) { + /* XXX should recover state? */ + ret = E2BIG; + goto err; + } + + memcpy(s, buf, len); + *nresult = (size_t)len; + return (0); + +err: + /* bound check failure */ + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_ISO2022_stdenc_wctocs(_ISO2022EncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + wchar_t m, nm; + + _DIAGASSERT(csid != NULL && idx != NULL); + + m = wc & 0x7FFF8080; + nm = wc & 0x007F7F7F; + if (m & 0x00800000) { + nm &= 0x00007F7F; + } else { + m &= 0x7F008080; + } + if (nm & 0x007F0000) { + /* ^3 mark */ + m |= 0x007F0000; + } else if (nm & 0x00007F00) { + /* ^2 mark */ + m |= 0x00007F00; + } + *csid = (_csid_t)m; + *idx = (_index_t)nm; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_ISO2022_stdenc_cstowc(_ISO2022EncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(ei != NULL && wc != NULL); + + *wc = (wchar_t)(csid & 0x7F808080) | (wchar_t)idx; + + return (0); +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(ISO2022); +_CITRUS_CTYPE_DEF_OPS(ISO2022); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(ISO2022); +_CITRUS_STDENC_DEF_OPS(ISO2022); + +#include "citrus_stdenc_template.h" diff --git a/lib/libc/citrus/modules/citrus_iso2022.h b/lib/libc/citrus/modules/citrus_iso2022.h new file mode 100644 index 0000000000..172333aba0 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_iso2022.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_iso2022.h,v 1.2 2003/06/25 09:51:44 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_iso2022.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_ISO2022_H_ +#define _CITRUS_ISO2022_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(ISO2022); +_CITRUS_STDENC_GETOPS_FUNC(ISO2022); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_mapper_646.c b/lib/libc/citrus/modules/citrus_mapper_646.c new file mode 100644 index 0000000000..967d92314f --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_646.c @@ -0,0 +1,256 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_646.c,v 1.4 2003/07/14 11:37:49 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_646.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_mapper_646.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_MAPPER_DECLS(mapper_646); +_CITRUS_MAPPER_DEF_OPS(mapper_646); + +/* ---------------------------------------------------------------------- */ + +#define ILSEQ 0xFFFFFFFE +#define INVALID 0xFFFFFFFF +#define SPECIALS(x) \ + x(0x23) \ + x(0x24) \ + x(0x40) \ + x(0x5B) \ + x(0x5C) \ + x(0x5D) \ + x(0x5E) \ + x(0x60) \ + x(0x7B) \ + x(0x7C) \ + x(0x7D) \ + x(0x7E) + +#define INDEX(x) INDEX_##x, + +enum { + SPECIALS(INDEX) + NUM_OF_SPECIALS +}; +struct _citrus_mapper_646 { + int m6_forward; + _index_t m6_map[NUM_OF_SPECIALS]; +}; + +int +_citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops *ops, + size_t lenops, uint32_t expected_version) +{ + if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenopsm6_map[i] = strtoul(buf, (char **)&p, 0); + p = _bcs_skip_ws(buf); + if (*p != T_COMM && !*p) { + ret = EINVAL; + break; + } + } + _unmap_file(&r); + + return ret; +}; + +static int +parse_var(struct _citrus_mapper_646 *m6, struct _memstream *ms, + const char *dir) +{ + struct _region r; + char path[PATH_MAX]; + + m6->m6_forward = 1; + _memstream_skip_ws(ms); + /* whether backward */ + if (_memstream_peek(ms) == '!') { + _memstream_getc(ms); + m6->m6_forward = 0; + } + /* get description file path */ + _memstream_getregion(ms, &r, _memstream_remainder(ms)); + snprintf(path, sizeof(path), "%s/%.*s", + dir, (int)_region_size(&r), (char *)_region_head(&r)); + /* remove trailing white spaces */ + path[_bcs_skip_nonws(path)-path] = '\0'; + return parse_file(m6, path); +} + +static int +/*ARGSUSED*/ +_citrus_mapper_646_mapper_init(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict cm, + const char * __restrict dir, + const void * __restrict var, size_t lenvar, + struct _citrus_mapper_traits * __restrict mt, + size_t lenmt) +{ + struct _citrus_mapper_646 *m6; + struct _memstream ms; + struct _region r; + int ret; + + _DIAGASSERT(cm && dir && mt); + + if (lenmtcm_closure = m6; + mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ + mt->mt_state_size = 0; /* stateless */ + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_646_mapper_uninit(struct _citrus_mapper *cm) +{ + if (cm && cm->cm_closure) { + free(cm->cm_closure); + } +} + +static int +/*ARGSUSED*/ +_citrus_mapper_646_mapper_convert(struct _citrus_mapper * __restrict cm, + _index_t * __restrict dst, _index_t src, + void * __restrict ps) +{ + struct _citrus_mapper_646 *m6; + + _DIAGASSERT(cm && cm->cm_closure); + + m6 = cm->cm_closure; + if (m6->m6_forward) { + /* forward */ + if (src>=0x80) + return _MAPPER_CONVERT_ILSEQ; +#define FORWARD(x) \ +if (src==(x)) { \ + if (m6->m6_map[INDEX_##x]==INVALID) \ + return _MAPPER_CONVERT_NONIDENTICAL; \ + *dst = m6->m6_map[INDEX_##x]; \ + return 0; \ +} else + SPECIALS(FORWARD); + *dst = src; + } else { + /* backward */ +#define BACKWARD(x) \ +if (m6->m6_map[INDEX_##x]!=INVALID && src==m6->m6_map[INDEX_##x]) { \ + *dst = (x); \ + return 0; \ +} else if (src==(x)) \ + return _MAPPER_CONVERT_ILSEQ; \ +else + SPECIALS(BACKWARD); + if (src>=0x80) + return _MAPPER_CONVERT_NONIDENTICAL; + *dst = src; + } + + return _MAPPER_CONVERT_SUCCESS; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_646_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ +} diff --git a/lib/libc/citrus/modules/citrus_mapper_646.h b/lib/libc/citrus/modules/citrus_mapper_646.h new file mode 100644 index 0000000000..69383a841d --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_646.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_646.h,v 1.1 2003/06/25 09:51:45 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_646.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_MAPPER_646_H_ +#define _CITRUS_MAPPER_646_H_ + +__BEGIN_DECLS +_CITRUS_MAPPER_GETOPS_FUNC(mapper_646); +_CITRUS_MAPPER_GETOPS_FUNC(mapper_646); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_mapper_none.c b/lib/libc/citrus/modules/citrus_mapper_none.c new file mode 100644 index 0000000000..e1f09b1eca --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_none.c @@ -0,0 +1,108 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_none.c,v 1.2 2003/06/27 17:53:31 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_none.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_mapper_none.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_MAPPER_DECLS(mapper_none); +_CITRUS_MAPPER_DEF_OPS(mapper_none); + + +/* ---------------------------------------------------------------------- */ + +int +_citrus_mapper_none_mapper_getops(struct _citrus_mapper_ops *ops, + size_t lenops, uint32_t expected_version) +{ + if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenopscm_closure = NULL; + mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ + mt->mt_state_size = 0; /* stateless */ + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_none_mapper_uninit(struct _citrus_mapper *cm) +{ +} + +static int +/*ARGSUSED*/ +_citrus_mapper_none_mapper_convert(struct _citrus_mapper * __restrict cm, + _citrus_index_t * __restrict dst, + _citrus_index_t src, void * __restrict ps) +{ + *dst = src; + return _CITRUS_MAPPER_CONVERT_SUCCESS; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_none_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ +} diff --git a/lib/libc/citrus/modules/citrus_mapper_none.h b/lib/libc/citrus/modules/citrus_mapper_none.h new file mode 100644 index 0000000000..e5b6bed445 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_none.h @@ -0,0 +1,37 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_none.h,v 1.1 2003/06/25 09:51:45 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_none.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_MAPPER_NONE_H_ +#define _CITRUS_MAPPER_NONE_H_ + +__BEGIN_DECLS +_CITRUS_MAPPER_GETOPS_FUNC(mapper_none); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_mapper_serial.c b/lib/libc/citrus/modules/citrus_mapper_serial.c new file mode 100644 index 0000000000..0677504a8e --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_serial.c @@ -0,0 +1,260 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_serial.c,v 1.2 2003/07/12 15:39:20 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_serial.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_mapper_serial.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_MAPPER_DECLS(mapper_serial); +_CITRUS_MAPPER_DEF_OPS(mapper_serial); + +#define _citrus_mapper_parallel_mapper_init \ + _citrus_mapper_serial_mapper_init +#define _citrus_mapper_parallel_mapper_uninit \ + _citrus_mapper_serial_mapper_uninit +#define _citrus_mapper_parallel_mapper_init_state \ + _citrus_mapper_serial_mapper_init_state +static int _citrus_mapper_parallel_mapper_convert( + struct _citrus_mapper * __restrict, _index_t * __restrict, _index_t, + void * __restrict); +_CITRUS_MAPPER_DEF_OPS(mapper_parallel); +#undef _citrus_mapper_parallel_mapper_init +#undef _citrus_mapper_parallel_mapper_uninit +#undef _citrus_mapper_parallel_mapper_init_state + + +/* ---------------------------------------------------------------------- */ + +struct maplink { + STAILQ_ENTRY(maplink) ml_entry; + struct _mapper *ml_mapper; +}; +STAILQ_HEAD(maplist, maplink); + +struct _citrus_mapper_serial { + struct maplist sr_mappers; +}; + +int +_citrus_mapper_serial_mapper_getops(struct _citrus_mapper_ops *ops, + size_t lenops, uint32_t expected_version) +{ + if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenopssr_mappers)) != NULL) { + STAILQ_REMOVE_HEAD(&sr->sr_mappers, ml_entry); + _mapper_close(ml->ml_mapper); + free(ml); + } +} + +static int +parse_var(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper_serial *sr, struct _memstream *ms) +{ + int ret; + struct _region r; + char mapname[PATH_MAX]; + struct maplink *ml; + + STAILQ_INIT(&sr->sr_mappers); + while (1) { + /* remove beginning white spaces */ + _memstream_skip_ws(ms); + if (_memstream_iseof(ms)) + break; + /* cut down a mapper name */ + _memstream_chr(ms, &r, ','); + snprintf(mapname, sizeof(mapname), "%.*s", + (int)_region_size(&r), (char *)_region_head(&r)); + /* remove trailing white spaces */ + mapname[_bcs_skip_nonws(mapname)-mapname] = '\0'; + /* create a new mapper record */ + ml = malloc(sizeof(*ml)); + if (ml == NULL) + return errno; + ret = _mapper_open(ma, &ml->ml_mapper, mapname); + if (ret) { + free(ml); + return ret; + } + /* support only 1:1 and stateless converter */ + if (_mapper_get_src_max(ml->ml_mapper) != 1 || + _mapper_get_dst_max(ml->ml_mapper) != 1 || + _mapper_get_state_size(ml->ml_mapper) != 0) { + free(ml); + return EINVAL; + } + STAILQ_INSERT_TAIL(&sr->sr_mappers, ml, ml_entry); + } + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_mapper_serial_mapper_init(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict cm, + const char * __restrict dir, + const void * __restrict var, size_t lenvar, + struct _citrus_mapper_traits * __restrict mt, + size_t lenmt) +{ + struct _citrus_mapper_serial *sr; + struct _memstream ms; + struct _region r; + + _DIAGASSERT(cm && dir && mt); + + if (lenmtcm_closure = sr; + mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ + mt->mt_state_size = 0; /* stateless */ + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_serial_mapper_uninit(struct _citrus_mapper *cm) +{ + if (cm && cm->cm_closure) { + uninit(cm->cm_closure); + free(cm->cm_closure); + } +} + +static int +/*ARGSUSED*/ +_citrus_mapper_serial_mapper_convert(struct _citrus_mapper * __restrict cm, + _index_t * __restrict dst, _index_t src, + void * __restrict ps) +{ + int ret; + struct _citrus_mapper_serial *sr; + struct maplink *ml; + + _DIAGASSERT(cm && cm->cm_closure); + + sr = cm->cm_closure; + STAILQ_FOREACH(ml, &sr->sr_mappers, ml_entry) { + ret = _mapper_convert(ml->ml_mapper, &src, src, NULL); + if (ret != _MAPPER_CONVERT_SUCCESS) + return ret; + } + *dst = src; + return _MAPPER_CONVERT_SUCCESS; +} + +static int +/*ARGSUSED*/ +_citrus_mapper_parallel_mapper_convert(struct _citrus_mapper * __restrict cm, + _index_t * __restrict dst, _index_t src, + void * __restrict ps) +{ + int ret; + struct _citrus_mapper_serial *sr; + struct maplink *ml; + _index_t tmp; + + _DIAGASSERT(cm && cm->cm_closure); + + sr = cm->cm_closure; + STAILQ_FOREACH(ml, &sr->sr_mappers, ml_entry) { + ret = _mapper_convert(ml->ml_mapper, &tmp, src, NULL); + if (ret == _MAPPER_CONVERT_SUCCESS) { + *dst = tmp; + return _MAPPER_CONVERT_SUCCESS; + } else if (ret == _MAPPER_CONVERT_ILSEQ) + return _MAPPER_CONVERT_ILSEQ; + } + return _MAPPER_CONVERT_NONIDENTICAL; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_serial_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ +} diff --git a/lib/libc/citrus/modules/citrus_mapper_serial.h b/lib/libc/citrus/modules/citrus_mapper_serial.h new file mode 100644 index 0000000000..9055c54b14 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_serial.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_serial.h,v 1.1 2003/06/25 09:51:46 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_serial.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_MAPPER_SERIAL_H_ +#define _CITRUS_MAPPER_SERIAL_H_ + +__BEGIN_DECLS +_CITRUS_MAPPER_GETOPS_FUNC(mapper_serial); +_CITRUS_MAPPER_GETOPS_FUNC(mapper_parallel); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_mapper_std.c b/lib/libc/citrus/modules/citrus_mapper_std.c new file mode 100644 index 0000000000..690eb90a3e --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_std.c @@ -0,0 +1,342 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_std.c,v 1.4 2004/12/21 11:25:43 yamt Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_std.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_region.h" +#include "citrus_mmap.h" +#include "citrus_module.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_db.h" +#include "citrus_db_hash.h" + +#include "citrus_mapper_std.h" +#include "citrus_mapper_std_file.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_MAPPER_DECLS(mapper_std); +_CITRUS_MAPPER_DEF_OPS(mapper_std); + + +/* ---------------------------------------------------------------------- */ + +int +_citrus_mapper_std_mapper_getops(struct _citrus_mapper_ops *ops, size_t lenops, + u_int32_t expected_version) +{ + if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenopsms_rowcol; + _index_t row, col, idx; + u_int32_t conv; + + if (rc->rc_src_col_bits == 32) { + row = 0; + col = src; + } else { + row = src >> rc->rc_src_col_bits; + col = src & ((1U<rc_src_col_bits)-1); + } + if (row < rc->rc_src_row_begin || row > rc->rc_src_row_end || + col < rc->rc_src_col_begin || col > rc->rc_src_col_end) { + switch (rc->rc_oob_mode) { + case _CITRUS_MAPPER_STD_OOB_NONIDENTICAL: + *dst = rc->rc_dst_invalid; + return _MAPPER_CONVERT_NONIDENTICAL; + case _CITRUS_MAPPER_STD_OOB_ILSEQ: + return _MAPPER_CONVERT_ILSEQ; + default: + return _MAPPER_CONVERT_FATAL; + } + } + + idx = + (row - rc->rc_src_row_begin)*rc->rc_src_col_width + + (col - rc->rc_src_col_begin); + + switch (rc->rc_dst_unit_bits) { + case 8: + conv = _region_peek8(&rc->rc_table, idx); + break; + case 16: + conv = be16toh(_region_peek16(&rc->rc_table, idx*2)); + break; + case 32: + conv = be32toh(_region_peek32(&rc->rc_table, idx*4)); + break; + } + + if (conv == rc->rc_dst_invalid) { + *dst = rc->rc_dst_invalid; + return _MAPPER_CONVERT_NONIDENTICAL; + } + if (conv == rc->rc_dst_ilseq) + return _MAPPER_CONVERT_ILSEQ; + + *dst = conv; + + return _MAPPER_CONVERT_SUCCESS; +} + + +static int +rowcol_init(struct _citrus_mapper_std *ms) +{ + int ret; + struct _citrus_mapper_std_rowcol *rc = &ms->ms_rowcol; + const struct _citrus_mapper_std_rowcol_info_x *rcx; + const struct _citrus_mapper_std_rowcol_ext_ilseq_info_x *eix; + struct _region r; + u_int64_t table_size; + + ms->ms_convert = &rowcol_convert; + ms->ms_uninit = NULL; + + /* get table region */ + ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TABLE, + &rc->rc_table, NULL); + if (ret) { + if (ret==ENOENT) + ret = EFTYPE; + return ret; + } + + /* get table information */ + ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_INFO, &r, NULL); + if (ret) { + if (ret==ENOENT) + ret = EFTYPE; + return ret; + } + if (_region_size(&r) < sizeof(*rcx)) + return EFTYPE; + rcx = _region_head(&r); + + /* convert */ +#define CONV_ROWCOL(rc, rcx, elem) \ +do { \ + (rc)->rc_##elem = be32toh((rcx)->rcx_##elem); \ +} while (/*CONSTCOND*/0) + CONV_ROWCOL(rc, rcx, src_col_bits); + CONV_ROWCOL(rc, rcx, dst_invalid); + CONV_ROWCOL(rc, rcx, src_row_begin); + CONV_ROWCOL(rc, rcx, src_row_end); + CONV_ROWCOL(rc, rcx, src_col_begin); + CONV_ROWCOL(rc, rcx, src_col_end); + CONV_ROWCOL(rc, rcx, dst_unit_bits); + + /* ilseq extension */ + rc->rc_oob_mode = _CITRUS_MAPPER_STD_OOB_NONIDENTICAL; + rc->rc_dst_ilseq = rc->rc_dst_invalid; + ret = _db_lookup_by_s(ms->ms_db, + _CITRUS_MAPPER_STD_SYM_ROWCOL_EXT_ILSEQ, + &r, NULL); + if (ret && ret != ENOENT) + return ret; + if (_region_size(&r) < sizeof(*eix)) + return EFTYPE; + if (ret == 0) { + eix = _region_head(&r); + rc->rc_oob_mode = be32toh(eix->eix_oob_mode); + rc->rc_dst_ilseq = be32toh(eix->eix_dst_ilseq); + } + rc->rc_src_col_width = rc->rc_src_col_end - rc->rc_src_col_begin +1; + + /* validation checks */ + if (rc->rc_src_col_end < rc->rc_src_col_begin || + rc->rc_src_row_end < rc->rc_src_row_begin || + !(rc->rc_dst_unit_bits==8 || rc->rc_dst_unit_bits==16 || + rc->rc_dst_unit_bits==32) || + !(rc->rc_src_col_bits >= 0 && rc->rc_src_col_bits <= 32)) + return EFTYPE; + + /* calcurate expected table size */ + table_size = rc->rc_src_row_end - rc->rc_src_row_begin + 1; + table_size *= rc->rc_src_col_width; + table_size *= rc->rc_dst_unit_bits/8; + + if (table_size > UINT32_MAX || + _region_size(&rc->rc_table) < table_size) + return EFTYPE; + + return 0; +} + +typedef int (*initfunc_t)(struct _citrus_mapper_std *); +static const struct { + const char *t_name; + initfunc_t t_init; +} types[] = { + { _CITRUS_MAPPER_STD_TYPE_ROWCOL, &rowcol_init }, +}; +#define NUM_OF_TYPES ((int)(sizeof(types)/sizeof(types[0]))) + +static int +/*ARGSUSED*/ +_citrus_mapper_std_mapper_init(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict cm, + const char * __restrict curdir, + const void * __restrict var, size_t lenvar, + struct _citrus_mapper_traits * __restrict mt, + size_t lenmt) +{ + char path[PATH_MAX]; + const char *type; + int ret, id; + struct _citrus_mapper_std *ms; + + /* set traits */ + if (lenmtmt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ + mt->mt_state_size = 0; /* stateless */ + + /* alloc mapper std structure */ + ms = malloc(sizeof(*ms)); + if (ms==NULL) { + ret = errno; + goto err0; + } + + /* open mapper file */ + snprintf(path, sizeof(path), + "%s/%.*s", curdir, (int)lenvar, (const char *)var); + ret = _map_file(&ms->ms_file, path); + if (ret) + goto err1; + + ret = _db_open(&ms->ms_db, &ms->ms_file, _CITRUS_MAPPER_STD_MAGIC, + &_db_hash_std, NULL); + if (ret) + goto err2; + + /* get mapper type */ + ret = _db_lookupstr_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TYPE, + &type, NULL); + if (ret) { + if (ret==ENOENT) + ret = EFTYPE; + goto err3; + } + for (id=0; idcm_closure = ms; + + return 0; + +err3: + _db_close(ms->ms_db); +err2: + _unmap_file(&ms->ms_file); +err1: + free(ms); +err0: + return ret; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_std_mapper_uninit(struct _citrus_mapper *cm) +{ + struct _citrus_mapper_std *ms; + + _DIAGASSERT(cm!=NULL & cm->cm_closure!=NULL); + + ms = cm->cm_closure; + if (ms->ms_uninit) + (*ms->ms_uninit)(ms); + _db_close(ms->ms_db); + _unmap_file(&ms->ms_file); + free(ms); +} + +static void +/*ARGSUSED*/ +_citrus_mapper_std_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ +} + +static int +/*ARGSUSED*/ +_citrus_mapper_std_mapper_convert(struct _citrus_mapper * __restrict cm, + _index_t * __restrict dst, _index_t src, + void * __restrict ps) +{ + struct _citrus_mapper_std *ms; + + _DIAGASSERT(cm!=NULL && cm->cm_closure!=NULL); + + ms = cm->cm_closure; + + _DIAGASSERT(ms->ms_convert != NULL); + + return (*ms->ms_convert)(ms, dst, src, ps); +} diff --git a/lib/libc/citrus/modules/citrus_mapper_std.h b/lib/libc/citrus/modules/citrus_mapper_std.h new file mode 100644 index 0000000000..dc1664f2dd --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_std.h @@ -0,0 +1,39 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_std.h,v 1.1 2003/06/25 09:51:47 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_std.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_MAPPER_STD_H_ +#define _CITRUS_MAPPER_STD_H_ + +#include "citrus_mapper_std_local.h" + +__BEGIN_DECLS +_CITRUS_MAPPER_GETOPS_FUNC(mapper_std); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_mapper_std_file.h b/lib/libc/citrus/modules/citrus_mapper_std_file.h new file mode 100644 index 0000000000..f0d019651c --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_std_file.h @@ -0,0 +1,62 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_std_file.h,v 1.2 2003/07/12 15:39:20 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_std_file.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_MAPPER_STD_FILE_H_ +#define _CITRUS_MAPPER_STD_FILE_H_ + +#define _CITRUS_MAPPER_STD_MAGIC "MAPPER\0\0" + +#define _CITRUS_MAPPER_STD_SYM_TYPE "type" +#define _CITRUS_MAPPER_STD_SYM_INFO "info" +#define _CITRUS_MAPPER_STD_SYM_TABLE "table" + +#define _CITRUS_MAPPER_STD_TYPE_ROWCOL "rowcol" +struct _citrus_mapper_std_rowcol_info_x { + u_int32_t rcx_src_col_bits; + u_int32_t rcx_dst_invalid; + u_int32_t rcx_src_row_begin; + u_int32_t rcx_src_row_end; + u_int32_t rcx_src_col_begin; + u_int32_t rcx_src_col_end; + u_int32_t rcx_dst_unit_bits; + u_int32_t rcx_pad; +} __packed; +#define _CITRUS_MAPPER_STD_ROWCOL_INFO_SIZE 32 + +/* rowcol oob extension info */ +#define _CITRUS_MAPPER_STD_SYM_ROWCOL_EXT_ILSEQ "rowcol_ext_ilseq" +struct _citrus_mapper_std_rowcol_ext_ilseq_info_x { +#define _CITRUS_MAPPER_STD_OOB_NONIDENTICAL 0 +#define _CITRUS_MAPPER_STD_OOB_ILSEQ 1 + u_int32_t eix_oob_mode; + u_int32_t eix_dst_ilseq; +} __packed; +#define _CITRUS_MAPPER_STD_ROWCOL_EXT_ILSEQ_SIZE 8 + +#endif diff --git a/lib/libc/citrus/modules/citrus_mapper_std_local.h b/lib/libc/citrus/modules/citrus_mapper_std_local.h new file mode 100644 index 0000000000..faa75699de --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_std_local.h @@ -0,0 +1,67 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_std_local.h,v 1.2 2003/07/12 15:39:21 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_std_local.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_MAPPER_STD_LOCAL_H_ +#define _CITRUS_MAPPER_STD_LOCAL_H_ + +typedef u_int32_t (*_citrus_mapper_std_getvalfunc_t)(const void *, u_int32_t); + +struct _citrus_mapper_std_rowcol { + struct _citrus_region rc_table; + int rc_src_col_bits; + _citrus_index_t rc_dst_invalid; + _citrus_index_t rc_src_row_begin; + _citrus_index_t rc_src_row_end; + _citrus_index_t rc_src_col_begin; + _citrus_index_t rc_src_col_end; + _citrus_index_t rc_src_col_width; + _citrus_index_t rc_dst_unit_bits; + int rc_oob_mode; + _citrus_index_t rc_dst_ilseq; +}; + +struct _citrus_mapper_std; + +typedef int (*_citrus_mapper_std_convert_t)( + struct _citrus_mapper_std *__restrict, + _index_t *__restrict, _index_t, void *__restrict); +typedef void (*_citrus_mapper_std_uninit_t)(struct _citrus_mapper_std *); + +struct _citrus_mapper_std { + struct _citrus_region ms_file; + struct _citrus_db *ms_db; + _citrus_mapper_std_convert_t ms_convert; + _citrus_mapper_std_uninit_t ms_uninit; + union { + struct _citrus_mapper_std_rowcol rowcol; + } u; +#define ms_rowcol u.rowcol +}; + +#endif diff --git a/lib/libc/citrus/modules/citrus_mapper_zone.c b/lib/libc/citrus/modules/citrus_mapper_zone.c new file mode 100644 index 0000000000..1dbf24cc08 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_zone.c @@ -0,0 +1,398 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_zone.c,v 1.4 2003/07/12 15:39:21 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_zone.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_bcs.h" +#include "citrus_module.h" +#include "citrus_region.h" +#include "citrus_memstream.h" +#include "citrus_mmap.h" +#include "citrus_hash.h" +#include "citrus_mapper.h" +#include "citrus_mapper_zone.h" + +/* ---------------------------------------------------------------------- */ + +_CITRUS_MAPPER_DECLS(mapper_zone); +_CITRUS_MAPPER_DEF_OPS(mapper_zone); + + +/* ---------------------------------------------------------------------- */ + +struct _zone { + u_int32_t z_begin; + u_int32_t z_end; +}; + +struct _citrus_mapper_zone { + struct _zone mz_row; + struct _zone mz_col; + int mz_col_bits; + int32_t mz_row_offset; + int32_t mz_col_offset; +}; + +struct _parse_state { + enum { S_BEGIN, S_OFFSET } ps_state; + union { + u_int32_t u_imm; + int32_t s_imm; + struct _zone zone; + } u; +#define ps_u_imm u.u_imm +#define ps_s_imm u.s_imm +#define ps_zone u.zone + int ps_top; +}; + +int +_citrus_mapper_zone_mapper_getops(struct _citrus_mapper_ops *ops, + size_t lenops, uint32_t expected_version) +{ + if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenopsps_u_imm = strtoul(buf, &p, 0); + if ((p-buf) != i) + return T_ERR; + if (sign == '-') + ps->ps_u_imm = (unsigned long)-(long)ps->ps_u_imm; + return T_IMM; +} + +static int +get_tok(struct _memstream *ms, struct _parse_state *ps) +{ + int c; + +loop: + c = _memstream_peek(ms); + if (c==0x00) + return EOF; + if (_bcs_isspace(c)) { + _memstream_getc(ms); + goto loop; + } + + switch (ps->ps_state) { + case S_BEGIN: + switch (c) { + case ':': + case '-': + case '/': + _memstream_getc(ms); + return c; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return get_imm(ms, ps); + } + break; + case S_OFFSET: + switch (c) { + case '/': + _memstream_getc(ms); + return c; + case '+': + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return get_imm(ms, ps); + } + break; + } + return T_ERR; +} + +static int +parse_zone(struct _memstream *ms, struct _parse_state *ps, struct _zone *z) +{ + if (get_tok(ms, ps) != T_IMM) + return -1; + z->z_begin = ps->ps_u_imm; + if (get_tok(ms, ps) != '-') + return -1; + if (get_tok(ms, ps) != T_IMM) + return -1; + z->z_end = ps->ps_u_imm; + + if (z->z_begin > z->z_end) + return -1; + + return 0; +} + +static int +check_rowcol(struct _zone *z, int32_t ofs, uint32_t maxval) +{ + u_int32_t remain; + + if (maxval != 0 && z->z_end >= maxval) + return -1; + + if (ofs > 0) { + if (maxval == 0) { + /* this should 0x100000000 - z->z_end */ + if (z->z_end == 0) { + remain = 0xFFFFFFFF; + } else { + remain = 0xFFFFFFFF - z->z_end + 1; + } + } else + remain = maxval - z->z_end; + if ((u_int32_t)ofs > remain) + return -1; + } else if (ofs < 0) { + if (z->z_begin < (u_int32_t)-ofs) + return -1; + } + + return 0; +} + +static int +parse_var(struct _citrus_mapper_zone *mz, struct _memstream *ms) +{ + struct _parse_state ps; + int ret, isrc; + uint32_t rowmax, colmax; + + ps.ps_state = S_BEGIN; + + if (parse_zone(ms, &ps, &mz->mz_col)) + return -1; + + ret = get_tok(ms, &ps); + if (ret == '/') { + /* rowzone / colzone / bits */ + isrc = 1; + mz->mz_row = mz->mz_col; + + if (parse_zone(ms, &ps, &mz->mz_col)) + return -1; + if (get_tok(ms, &ps) != '/') + return -1; + if (get_tok(ms, &ps) != T_IMM) + return -1; + mz->mz_col_bits = ps.ps_u_imm; + if (mz->mz_col_bits<0 || mz->mz_col_bits>32) + return -1; + ret = get_tok(ms, &ps); + } else { + /* colzone */ + isrc = 0; + mz->mz_col_bits = 32; + mz->mz_row.z_begin = mz->mz_row.z_end = 0; + } + if (ret == ':') { + /* offset */ + ps.ps_state = S_OFFSET; + if (get_tok(ms, &ps) != T_IMM) + return -1; + mz->mz_col_offset = ps.ps_s_imm; + if (isrc) { + /* row/col */ + mz->mz_row_offset = mz->mz_col_offset; + if (get_tok(ms, &ps) != '/') + return -1; + if (get_tok(ms, &ps) != T_IMM) + return -1; + mz->mz_col_offset = ps.ps_s_imm; + } else + mz->mz_row_offset = 0; + ret = get_tok(ms, &ps); + } + if (ret != EOF) + return -1; + + /* sanity check */ + if (mz->mz_col_bits==32) + colmax = 0; + else + colmax = 1 << mz->mz_col_bits; + if (mz->mz_col_bits==0) + rowmax = 0; + else + rowmax = 1 << (32-mz->mz_col_bits); + if (check_rowcol(&mz->mz_col, mz->mz_col_offset, colmax)) + return -1; + if (check_rowcol(&mz->mz_row, mz->mz_row_offset, rowmax)) + return -1; + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_mapper_zone_mapper_init(struct _citrus_mapper_area *__restrict ma, + struct _citrus_mapper * __restrict cm, + const char * __restrict dir, + const void * __restrict var, size_t lenvar, + struct _citrus_mapper_traits * __restrict mt, + size_t lenmt) +{ + struct _citrus_mapper_zone *mz; + struct _memstream ms; + struct _region r; + + _DIAGASSERT(cm && dir && mt); + + if (lenmtmz_col.z_begin = mz->mz_col.z_end = 0; + mz->mz_row.z_begin = mz->mz_row.z_end = 0; + mz->mz_col_bits = 0; + mz->mz_row_offset = 0; + mz->mz_col_offset = 0; + + _region_init(&r, (void *)var, lenvar); + _memstream_bind(&ms, &r); + if (parse_var(mz, &ms)) { + free(mz); + return EINVAL; + } + cm->cm_closure = mz; + mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ + mt->mt_state_size = 0; /* stateless */ + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_zone_mapper_uninit(struct _citrus_mapper *cm) +{ +} + +static int +/*ARGSUSED*/ +_citrus_mapper_zone_mapper_convert(struct _citrus_mapper * __restrict cm, + _citrus_index_t * __restrict dst, + _citrus_index_t src, void * __restrict ps) +{ + u_int32_t row, col; + struct _citrus_mapper_zone *mz = cm->cm_closure; + + if (mz->mz_col_bits == 32) { + col = src; + row = 0; + if (col < mz->mz_col.z_begin || col > mz->mz_col.z_end) + return _CITRUS_MAPPER_CONVERT_NONIDENTICAL; + if (mz->mz_col_offset>0) + col += (u_int32_t)mz->mz_col_offset; + else + col -= (u_int32_t)-mz->mz_col_offset; + *dst = col; + } else { + col = src & (((u_int32_t)1<mz_col_bits)-1); + row = src >> mz->mz_col_bits; + if (row < mz->mz_row.z_begin || row > mz->mz_row.z_end || + col < mz->mz_col.z_begin || col > mz->mz_col.z_end) + return _CITRUS_MAPPER_CONVERT_NONIDENTICAL; + if (mz->mz_col_offset>0) + col += (u_int32_t)mz->mz_col_offset; + else + col -= (u_int32_t)-mz->mz_col_offset; + if (mz->mz_row_offset>0) + row += (u_int32_t)mz->mz_row_offset; + else + row -= (u_int32_t)-mz->mz_row_offset; + *dst = col | (row << mz->mz_col_bits); + } + return _CITRUS_MAPPER_CONVERT_SUCCESS; +} + +static void +/*ARGSUSED*/ +_citrus_mapper_zone_mapper_init_state(struct _citrus_mapper * __restrict cm, + void * __restrict ps) +{ +} diff --git a/lib/libc/citrus/modules/citrus_mapper_zone.h b/lib/libc/citrus/modules/citrus_mapper_zone.h new file mode 100644 index 0000000000..2c99afff5b --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mapper_zone.h @@ -0,0 +1,37 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_zone.h,v 1.1 2003/06/25 09:51:48 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_zone.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_MAPPER_ZONE_H_ +#define _CITRUS_MAPPER_ZONE_H_ + +__BEGIN_DECLS +_CITRUS_MAPPER_GETOPS_FUNC(mapper_zone); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_mskanji.c b/lib/libc/citrus/modules/citrus_mskanji.c new file mode 100644 index 0000000000..5074992a76 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mskanji.c @@ -0,0 +1,455 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mskanji.c,v 1.9 2005/02/10 19:03:51 tnozaki Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mskanji.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * ja_JP.SJIS locale table for BSD4.4/rune + * version 1.0 + * (C) Sin'ichiro MIYATANI / Phase One, Inc + * May 12, 1995 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Phase One, Inc. + * 4. The name of Phase One, Inc. may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_mskanji.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct _MSKanjiState { + char ch[2]; + int chlen; +} _MSKanjiState; + +typedef struct { + int dummy; +} _MSKanjiEncodingInfo; + +typedef struct { + _MSKanjiEncodingInfo ei; + struct { + /* for future multi-locale facility */ + _MSKanjiState s_mblen; + _MSKanjiState s_mbrlen; + _MSKanjiState s_mbrtowc; + _MSKanjiState s_mbtowc; + _MSKanjiState s_mbsrtowcs; + _MSKanjiState s_wcrtomb; + _MSKanjiState s_wcsrtombs; + _MSKanjiState s_wctomb; + } states; +} _MSKanjiCTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_MSKanji_##m +#define _ENCODING_INFO _MSKanjiEncodingInfo +#define _CTYPE_INFO _MSKanjiCTypeInfo +#define _ENCODING_STATE _MSKanjiState +#define _ENCODING_MB_CUR_MAX(_ei_) 2 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static int +_mskanji1(int c) +{ + + if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xef)) + return 1; + else + return 0; +} + +static int +_mskanji2(int c) +{ + + if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc)) + return 1; + else + return 0; +} + +static __inline void +/*ARGSUSED*/ +_citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei, + _MSKanjiState * __restrict s) +{ + memset(s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei, + void * __restrict pspriv, + const _MSKanjiState * __restrict s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei, + _MSKanjiState * __restrict s, + const void * __restrict pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static int +/*ARGSUSED*/ +_citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei, + wchar_t * __restrict pwc, + const char ** __restrict s, size_t n, + _MSKanjiState * __restrict psenc, + size_t * __restrict nresult) +{ + wchar_t wchar; + int len; + int chlenbak; + const char *s0; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_MSKanji_init_state(ei, psenc); + *nresult = 0; /* state independent */ + return (0); + } + + chlenbak = psenc->chlen; + + /* make sure we have the first byte in the buffer */ + switch (psenc->chlen) { + case 0: + if (n < 1) + goto restart; + psenc->ch[0] = *s0++; + psenc->chlen = 1; + n--; + break; + case 1: + break; + default: + /* illegal state */ + goto encoding_error; + } + + len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1; + while (psenc->chlen < len) { + if (n < 1) + goto restart; + psenc->ch[psenc->chlen] = *s0++; + psenc->chlen++; + n--; + } + + *s = s0; + + switch (len) { + case 1: + wchar = psenc->ch[0] & 0xff; + break; + case 2: + if (!_mskanji2(psenc->ch[1] & 0xff)) + goto encoding_error; + wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); + break; + default: + /* illegal state */ + goto encoding_error; + } + + psenc->chlen = 0; + + if (pwc) + *pwc = wchar; + + if (!wchar) + *nresult = 0; + else + *nresult = len - chlenbak; + + return (0); + +encoding_error: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +restart: + *nresult = (size_t)-2; + *s = s0; + return (0); +} + + +static int +_citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wc, + _MSKanjiState * __restrict psenc, + size_t * __restrict nresult) +{ + int ret; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(s != NULL); + + /* check invalid sequence */ + if (wc & ~0xffff) { + ret = EILSEQ; + goto err; + } + + if (wc & 0xff00) { + if (n < 2) { + ret = E2BIG; + goto err; + } + + s[0] = (wc >> 8) & 0xff; + s[1] = wc & 0xff; + if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff)) { + ret = EILSEQ; + goto err; + } + + *nresult = 2; + return 0; + } else { + if (n < 1) { + ret = E2BIG; + goto err; + } + + s[0] = wc & 0xff; + if (_mskanji1(s[0] & 0xff)) { + ret = EILSEQ; + goto err; + } + + *nresult = 1; + return 0; + } + +err: + *nresult = (size_t)-1; + return ret; +} + + +static __inline int +/*ARGSUSED*/ +_citrus_MSKanji_stdenc_wctocs(_MSKanjiEncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + _index_t row, col; + + _DIAGASSERT(csid != NULL && idx != NULL); + + if ((_wc_t)wc < 0x80) { + /* ISO-646 */ + *csid = 0; + *idx = (_index_t)wc; + } else if ((_wc_t)wc < 0x100) { + /* KANA */ + *csid = 1; + *idx = (_index_t)wc & 0x7F; + } else if ((0x8140 <= (_wc_t)wc && (_wc_t)wc <= 0x9FFC) || + (0xE040 <= (_wc_t)wc && (_wc_t)wc <= 0xFCFC)) { + /* Kanji (containing Gaiji zone) */ + /* + * 94^2 zone (contains a part of Gaiji (0xED40 - 0xEEFC)): + * 0x8140 - 0x817E -> 0x2121 - 0x215F + * 0x8180 - 0x819E -> 0x2160 - 0x217E + * 0x819F - 0x81FC -> 0x2221 - 0x227E + * + * 0x8240 - 0x827E -> 0x2321 - 0x235F + * ... + * 0x9F9F - 0x9FFc -> 0x5E21 - 0x5E7E + * + * 0xE040 - 0xE07E -> 0x5F21 - 0x5F5F + * ... + * 0xEF9F - 0xEFFC -> 0x7E21 - 0x7E7E + * + * extended Gaiji zone: + * 0xF040 - 0xFCFC + */ + *csid = 2; + row = ((_wc_t)wc >> 8) - 0x81; + if (row >= 0x5F) + row -= 0x40; + row = row * 2 + 0x21; + col = (wc & 0xFF) - 0x1F; + if (col >= 0x61) + col -= 1; + if (col > 0x7E) { + row += 1; + col -= 0x5E; + } + *idx = ((_index_t)row << 8) | col; + } else + return EILSEQ; + + return 0; +} + +static __inline int +/*ARGSUSED*/ +_citrus_MSKanji_stdenc_cstowc(_MSKanjiEncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + u_int32_t row, col; + + _DIAGASSERT(wc != NULL); + + switch (csid) { + case 0: + /* ISO-646 */ + if (idx >= 0x80) + return EILSEQ; + *wc = (wchar_t)idx; + break; + case 1: + /* kana */ + if (idx >= 0x80) + return EILSEQ; + *wc = (wchar_t)idx + 0x80; + break; + case 2: + /* kanji */ + row = (idx >> 8); + col = idx & 0x7F; + if (row<0x21 || row>0x97 || col<0x21 || col>0x7E) + return EILSEQ; + row -= 0x21; col -= 0x21; + if ((row & 1)==0) { + col += 0x40; + if (col>=0x7F) + col += 1; + } else + col += 0x9F; + if (row<0x3E) + row = row/2 + 0x81; + else + row = row/2 + 0xc1; + *wc = ((wchar_t)row << 8) | col; + break; + default: + return EILSEQ; + } + + return 0; +} + +static int +/*ARGSUSED*/ +_citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo * __restrict ei, + const void * __restrict var, + size_t lenvar) +{ + _DIAGASSERT(ei != NULL); + + return (0); +} + +static void +_citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo *ei) +{ +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(MSKanji); +_CITRUS_CTYPE_DEF_OPS(MSKanji); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(MSKanji); +_CITRUS_STDENC_DEF_OPS(MSKanji); + +#include "citrus_stdenc_template.h" diff --git a/lib/libc/citrus/modules/citrus_mskanji.h b/lib/libc/citrus/modules/citrus_mskanji.h new file mode 100644 index 0000000000..e0afd97f93 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_mskanji.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_mskanji.h,v 1.2 2003/06/25 09:51:48 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_mskanji.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_MSKANJI_H_ +#define _CITRUS_MSKANJI_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(MSKanji); +_CITRUS_STDENC_GETOPS_FUNC(MSKanji); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_utf1632.c b/lib/libc/citrus/modules/citrus_utf1632.c new file mode 100644 index 0000000000..0576a3c6d8 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_utf1632.c @@ -0,0 +1,434 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_utf1632.c,v 1.3 2003/06/27 12:55:13 yamt Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_utf1632.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_stdenc.h" +#include "citrus_bcs.h" + +#include "citrus_utf1632.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + u_int8_t ch[4]; + int chlen; + int current_endian; +} _UTF1632State; + +typedef struct { + int preffered_endian; + unsigned int cur_max; +#define _ENDIAN_UNKNOWN 0 +#define _ENDIAN_BIG 1 +#define _ENDIAN_LITTLE 2 + u_int32_t mode; +#define _MODE_UTF32 0x00000001U +#define _MODE_FORCE_ENDIAN 0x00000002U +} _UTF1632EncodingInfo; + +#define _FUNCNAME(m) _citrus_UTF1632_##m +#define _ENCODING_INFO _UTF1632EncodingInfo +#define _ENCODING_STATE _UTF1632State +#define _ENCODING_MB_CUR_MAX(_ei_) ((_ei_)->cur_max) +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static __inline void +/*ARGSUSED*/ +_citrus_UTF1632_init_state(_UTF1632EncodingInfo *ei, _UTF1632State *s) +{ + memset(s, 0, sizeof(*s)); +} + +static int +_citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo *ei, wchar_t *pwc, + const char **s, size_t n, _UTF1632State *psenc, + size_t *nresult) +{ + int chlenbak, endian, needlen; + wchar_t wc; + size_t result; + const char *s0; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_UTF1632_init_state(ei, psenc); + *nresult = 0; /* state independent */ + return (0); + } + + result = 0; + chlenbak = psenc->chlen; + +refetch: + if ((ei->mode & _MODE_UTF32) != 0 || chlenbak>=2) + needlen = 4; + else + needlen = 2; + + while (chlenbak < needlen) { + if (n==0) + goto restart; + psenc->ch[chlenbak++] = *s0++; + n--; + result++; + } + + /* judge endian marker */ + if ((ei->mode & _MODE_UTF32) == 0) { + /* UTF16 */ + if (psenc->ch[0]==0xFE && psenc->ch[1]==0xFF) { + psenc->current_endian = _ENDIAN_BIG; + chlenbak = 0; + goto refetch; + } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE) { + psenc->current_endian = _ENDIAN_LITTLE; + chlenbak = 0; + goto refetch; + } + } else { + /* UTF32 */ + if (psenc->ch[0]==0x00 && psenc->ch[1]==0x00 && + psenc->ch[2]==0xFE && psenc->ch[3]==0xFF) { + psenc->current_endian = _ENDIAN_BIG; + chlenbak = 0; + goto refetch; + } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE && + psenc->ch[2]==0x00 && psenc->ch[3]==0x00) { + psenc->current_endian = _ENDIAN_LITTLE; + chlenbak = 0; + goto refetch; + } + } + if ((ei->mode & _MODE_FORCE_ENDIAN) != 0 || + psenc->current_endian == _ENDIAN_UNKNOWN) + endian = ei->preffered_endian; + else + endian = psenc->current_endian; + + /* get wc */ + if ((ei->mode & _MODE_UTF32) == 0) { + /* UTF16 */ + if (needlen==2) { + switch (endian) { + case _ENDIAN_LITTLE: + wc = (psenc->ch[0] | + ((wchar_t)psenc->ch[1] << 8)); + break; + case _ENDIAN_BIG: + wc = (psenc->ch[1] | + ((wchar_t)psenc->ch[0] << 8)); + break; + } + if (wc >= 0xD800 && wc <= 0xDBFF) { + /* surrogate high */ + needlen=4; + goto refetch; + } + } else { + /* surrogate low */ + wc -= 0xD800; /* wc : surrogate high (see above) */ + wc <<= 10; + switch (endian) { + case _ENDIAN_LITTLE: + if (psenc->ch[2]<0xDC || psenc->ch[2]>0xDF) + goto ilseq; + wc |= psenc->ch[2]; + wc |= (wchar_t)(psenc->ch[3] & 3) << 8; + break; + case _ENDIAN_BIG: + if (psenc->ch[3]<0xDC || psenc->ch[3]>0xDF) + goto ilseq; + wc |= psenc->ch[3]; + wc |= (wchar_t)(psenc->ch[2] & 3) << 8; + break; + } + wc += 0x10000; + } + } else { + /* UTF32 */ + switch (endian) { + case _ENDIAN_LITTLE: + wc = (psenc->ch[0] | + ((wchar_t)psenc->ch[1] << 8) | + ((wchar_t)psenc->ch[2] << 16) | + ((wchar_t)psenc->ch[3] << 24)); + break; + case _ENDIAN_BIG: + wc = (psenc->ch[3] | + ((wchar_t)psenc->ch[2] << 8) | + ((wchar_t)psenc->ch[1] << 16) | + ((wchar_t)psenc->ch[0] << 24)); + break; + } + } + + + *pwc = wc; + psenc->chlen = 0; + *nresult = result; + *s = s0; + + return (0); + +ilseq: + *nresult = (size_t)-1; + psenc->chlen = 0; + return (EILSEQ); + +restart: + *nresult = (size_t)-2; + psenc->chlen = chlenbak; + *s = s0; + return (0); +} + +static int +_citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo *ei, char *s, size_t n, + wchar_t wc, _UTF1632State *psenc, + size_t *nresult) +{ + int ret; + wchar_t wc2; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + wc2 = 0; + if ((ei->mode & _MODE_UTF32)==0) { + /* UTF16 */ + if (wc>0xFFFF) { + /* surrogate */ + if (wc>0x10FFFF) { + ret = EILSEQ; + goto err; + } + if (n < 4) { + ret = E2BIG; + goto err; + } + wc -= 0x10000; + wc2 = (wc & 0x3FF) | 0xDC00; + wc = (wc>>10) | 0xD800; + *nresult = (size_t)4; + } else { + if (n < 2) { + ret = E2BIG; + goto err; + } + *nresult = (size_t)2; + } + +surrogate: + switch (ei->preffered_endian) { + case _ENDIAN_BIG: + s[1] = wc; + s[0] = (wc >>= 8); + break; + case _ENDIAN_LITTLE: + s[0] = wc; + s[1] = (wc >>= 8); + break; + } + if (wc2!=0) { + wc = wc2; + wc2 = 0; + s += 2; + goto surrogate; + } + } else { + /* UTF32 */ + if (n < 4) { + ret = E2BIG; + goto err; + } + switch (ei->preffered_endian) { + case _ENDIAN_BIG: + s[3] = wc; + s[2] = (wc >>= 8); + s[1] = (wc >>= 8); + s[0] = (wc >>= 8); + break; + case _ENDIAN_LITTLE: + s[0] = wc; + s[1] = (wc >>= 8); + s[2] = (wc >>= 8); + s[3] = (wc >>= 8); + break; + } + *nresult = (size_t)4; + } + + return 0; + +err: + *nresult = (size_t)-1; + return ret; +} + +static void +parse_variable(_UTF1632EncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ +#define MATCH(x, act) \ +do { \ + if (lenvar >= (sizeof(#x)-1) && \ + _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \ + act; \ + lenvar -= sizeof(#x)-1; \ + p += sizeof(#x)-1; \ + } \ +} while (/*CONSTCOND*/0) + const char *p; + p = var; + while (lenvar>0) { + switch (*p) { + case 'B': + case 'b': + MATCH(big, ei->preffered_endian = _ENDIAN_BIG); + break; + case 'L': + case 'l': + MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE); + break; + case 'F': + case 'f': + MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN); + break; + case 'U': + case 'u': + MATCH(utf32, ei->mode |= _MODE_UTF32); + break; + } + p++; + lenvar--; + } +} + +static int +/*ARGSUSED*/ +_citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei, + const void * __restrict var, + size_t lenvar) +{ + _DIAGASSERT(ei != NULL); + + memset((void *)ei, 0, sizeof(*ei)); + + parse_variable(ei, var, lenvar); + + if ((ei->mode&_MODE_UTF32)==0) + ei->cur_max = 6; /* endian + surrogate */ + else + ei->cur_max = 8; /* endian + normal */ + + if (ei->preffered_endian == _ENDIAN_UNKNOWN) { +#if BYTE_ORDER == BIG_ENDIAN + ei->preffered_endian = _ENDIAN_BIG; +#else + ei->preffered_endian = _ENDIAN_LITTLE; +#endif + } + + return (0); +} + +static void +/*ARGSUSED*/ +_citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei) +{ +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, + _wc_t wc) +{ + + _DIAGASSERT(csid != NULL && idx != NULL); + + *csid = 0; + *idx = (_index_t)wc; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo * __restrict ei, + _wc_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(wc != NULL); + + if (csid != 0) + return (EILSEQ); + + *wc = (_wc_t)idx; + + return (0); +} + + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(UTF1632); +_CITRUS_STDENC_DEF_OPS(UTF1632); + +#include "citrus_stdenc_template.h" diff --git a/lib/libc/citrus/modules/citrus_utf1632.h b/lib/libc/citrus/modules/citrus_utf1632.h new file mode 100644 index 0000000000..3762ed1355 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_utf1632.h @@ -0,0 +1,37 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_utf1632.h,v 1.1 2003/06/25 09:51:49 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_utf1632.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_UTF1632_H_ +#define _CITRUS_UTF1632_H_ + +__BEGIN_DECLS +_CITRUS_STDENC_GETOPS_FUNC(UTF1632); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_utf7.c b/lib/libc/citrus/modules/citrus_utf7.c new file mode 100644 index 0000000000..91266a7b0b --- /dev/null +++ b/lib/libc/citrus/modules/citrus_utf7.c @@ -0,0 +1,574 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_utf7.c,v 1.1 2005/03/05 18:05:15 tnozaki Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_utf7.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2004, 2005 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_utf7.h" + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +typedef struct { + uint16_t cell[0x80]; +#define EI_MASK UINT16_C(0xff) +#define EI_DIRECT UINT16_C(0x100) +#define EI_OPTION UINT16_C(0x200) +#define EI_SPACE UINT16_C(0x400) +} _UTF7EncodingInfo; + +typedef struct { + unsigned int + chlen: 3, /* need to hold 0 - 4 */ + mode: 1, /* whether base64 mode */ + bits: 4, /* need to hold 0 - 15 */ + cache: 22, /* 22 = BASE64_BIT + UTF16_BIT */ + surrogate: 1, /* whether surrogate pair or not */ + unused: 1; +} _UTF7StatePriv; + +typedef struct { + union { + uint32_t chlen; + _UTF7StatePriv state; + }; + char ch[4]; /* BASE64_IN, 3 * 6 = 18, most closed to UTF16_BIT */ +} _UTF7State; + +typedef struct { + _UTF7EncodingInfo ei; + struct { + /* for future multi-locale facility */ + _UTF7State s_mblen; + _UTF7State s_mbrlen; + _UTF7State s_mbrtowc; + _UTF7State s_mbtowc; + _UTF7State s_mbsrtowcs; + _UTF7State s_wcrtomb; + _UTF7State s_wcsrtombs; + _UTF7State s_wctomb; + } states; +} _UTF7CTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_UTF7_##m +#define _ENCODING_INFO _UTF7EncodingInfo +#define _CTYPE_INFO _UTF7CTypeInfo +#define _ENCODING_STATE _UTF7State +#define _ENCODING_MB_CUR_MAX(_ei_) 4 +#define _ENCODING_IS_STATE_DEPENDENT 1 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + +static __inline void +/*ARGSUSED*/ +_citrus_UTF7_init_state(_UTF7EncodingInfo * __restrict ei, + _UTF7State * __restrict s) +{ + /* ei appears to be unused */ + _DIAGASSERT(s != NULL); + + memset((void *)s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_UTF7_pack_state(_UTF7EncodingInfo * __restrict ei, + void *__restrict pspriv, const _UTF7State * __restrict s) +{ + /* ei seem to be unused */ + _DIAGASSERT(pspriv != NULL); + _DIAGASSERT(s != NULL); + + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_UTF7_unpack_state(_UTF7EncodingInfo * __restrict ei, + _UTF7State * __restrict s, const void * __restrict pspriv) +{ + /* ei seem to be unused */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(pspriv != NULL); + + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static const char base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +static const char direct[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789(),-./:?"; + +static const char option[] = "!\"#$%&';<=>@[]^_`{|}"; +static const char spaces[] = " \t\r\n"; + +#define BASE64_BIT 6 +#define UTF16_BIT 16 + +#define BASE64_MAX 0x3f +#define UTF16_MAX UINT16_C(0xffff) +#define UTF32_MAX UINT32_C(0x10ffff) + +#define BASE64_IN '+' +#define BASE64_OUT '-' + +#define SHIFT7BIT(c) ((c) >> 7) +#define ISSPECIAL(c) ((c) == '\0' || (c) == BASE64_IN) + +#define FINDLEN(ei, c) \ + (SHIFT7BIT((c)) ? -1 : (((ei)->cell[(c)] & EI_MASK) - 1)) + +#define ISDIRECT(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \ + ei->cell[(c)] & (EI_DIRECT | EI_OPTION | EI_SPACE))) + +#define ISSAFE(ei, c) (!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \ + ei->cell[(c)] & (EI_DIRECT | EI_SPACE))) + +/* surrogate pair */ +#define SRG_BASE UINT32_C(0x10000) +#define HISRG_MIN UINT16_C(0xd800) +#define HISRG_MAX UINT16_C(0xdbff) +#define LOSRG_MIN UINT16_C(0xdc00) +#define LOSRG_MAX UINT16_C(0xdfff) +#define CHECK_SRG(st, c, act) \ +do { \ + if (!(st)->surrogate) { \ + if ((c) >= HISRG_MIN && (c) <= HISRG_MAX) \ + (st)->surrogate = 1; \ + } else { \ + if ((c) < LOSRG_MIN || (c) > LOSRG_MAX) \ + act; \ + (st)->surrogate = 0; \ + } \ +} while (/*CONSTCOND*/0) + +static int +_mbtoutf16(_UTF7EncodingInfo * __restrict ei, + uint16_t * __restrict u16, const char ** __restrict s, size_t n, + _UTF7State * __restrict psenc, size_t * __restrict nresult) +{ + _UTF7StatePriv *st, sv; + const char *s0; + int i, done, len; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL && *s != NULL); + _DIAGASSERT(psenc != NULL); + + s0 = *s; + sv = *(st = &psenc->state); + + for (i = 0, done = 0; done == 0; i++) { + _DIAGASSERT(i <= st->chlen); + if (i == st->chlen) { + if (n-- < 1) { + *nresult = (size_t)-2; + *s = s0; + sv.chlen = st->chlen; + *st = sv; + return (0); + } + psenc->ch[st->chlen++] = *s0++; + } + if (SHIFT7BIT((int)psenc->ch[i])) + goto ilseq; + if (!st->mode) { + if (st->bits > 0 || st->cache > 0) + return (EINVAL); + if (psenc->ch[i] == BASE64_IN) { + st->mode = 1; + } else { + if (!ISDIRECT(ei, (int)psenc->ch[i])) + goto ilseq; + *u16 = (uint16_t)psenc->ch[i]; + done = 1; + continue; + } + } else { + if (psenc->ch[i] == BASE64_OUT && st->cache == 0) { + st->mode = 0; + *u16 = (uint16_t)BASE64_IN; + done = 1; + continue; + } + len = FINDLEN(ei, (int)psenc->ch[i]); + if (len < 0) { + if (st->bits >= BASE64_BIT) + return (EINVAL); + st->mode = 0; + st->bits = st->cache = 0; + if (psenc->ch[i] != BASE64_OUT) { + if (!ISDIRECT(ei, (int)psenc->ch[i])) + goto ilseq; + *u16 = (uint16_t)psenc->ch[i]; + done = 1; + } + } else { + st->cache = (st->cache << BASE64_BIT) | len; + switch (st->bits) { + case 0: case 2: case 4: case 6: case 8: + st->bits += BASE64_BIT; + break; + case 10: case 12: case 14: + st->bits -= (UTF16_BIT - BASE64_BIT); + *u16 = (st->cache >> st->bits) + & UTF16_MAX; + CHECK_SRG(st, *u16, goto ilseq); + done = 1; + break; + default: + return (EINVAL); + } + } + } + } + + if (st->chlen > i) + return (EINVAL); + st->chlen = 0; + *nresult = (size_t)((*u16 == 0) ? 0 : s0 - *s); + *s = s0; + + return (0); + +ilseq: + *nresult = (size_t)-1; + return (EILSEQ); +} + +static int +_citrus_UTF7_mbrtowc_priv(_UTF7EncodingInfo * __restrict ei, + wchar_t * __restrict pwc, const char ** __restrict s, size_t n, + _UTF7State * __restrict psenc, size_t * __restrict nresult) +{ + _UTF7StatePriv *st; + uint32_t u32; + uint16_t hi, lo; + size_t siz; + int err; + + _DIAGASSERT(ei != NULL); + /* pwc may be null */ + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + if (*s == NULL) { + _citrus_UTF7_init_state(ei, psenc); + *nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT; + return (0); + } + + st = &psenc->state; + if (st->surrogate) { + hi = (st->cache >> 2) & UTF16_MAX; + if (hi >= HISRG_MIN && hi <= HISRG_MAX) + return (EINVAL); + siz = 0; + } else { + err = _mbtoutf16(ei, &hi, s, n, psenc, nresult); + if (err || *nresult == (size_t)-2) + return (err); + n -= *nresult; + siz = *nresult; + } + if (!st->surrogate) { + u32 = (uint32_t)hi; + } else { + err = _mbtoutf16(ei, &lo, s, n, psenc, nresult); + if (err || *nresult == (size_t)-2) + return (err); + _DIAGASSERT(!st->surrogate && + lo >= LOSRG_MIN && lo <= LOSRG_MAX); + hi -= HISRG_MIN; + lo -= LOSRG_MIN; + u32 = (hi << 10 | lo) + SRG_BASE; + *nresult += siz; + } + if (pwc != NULL) + *pwc = (wchar_t)u32; + + return (0); +} + +static __inline int +_utf16tomb(_UTF7EncodingInfo * __restrict ei, + uint16_t u16, _UTF7State * __restrict psenc) +{ + _UTF7StatePriv *st; + int bits, i; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(psenc != NULL); + + st = &psenc->state; + if (st->chlen != 0 || st->bits > BASE64_BIT) + return (EINVAL); + CHECK_SRG(st, u16, return (EILSEQ)); + + if (ISSAFE(ei, u16)) { + if (st->mode) { + if (st->bits > 0) { + bits = BASE64_BIT - st->bits; + i = (st->cache << bits) & BASE64_MAX; + psenc->ch[st->chlen++] = base64[i]; + st->bits = st->cache = 0; + } + if (u16 == BASE64_OUT || FINDLEN(ei, u16) >= 0) + psenc->ch[st->chlen++] = BASE64_OUT; + st->mode = 0; + } + if (st->bits != 0) + return (EINVAL); + psenc->ch[st->chlen++] = (char)u16; + if (u16 == BASE64_IN) + psenc->ch[st->chlen++] = BASE64_OUT; + } else { + if (!st->mode) { + if (st->bits > 0) + return (EINVAL); + psenc->ch[st->chlen++] = BASE64_IN; + st->mode = 1; + } + st->cache = (st->cache << UTF16_BIT) | u16; + bits = UTF16_BIT + st->bits; + st->bits = bits % BASE64_BIT; + while ((bits -= BASE64_BIT) >= 0) { + i = (st->cache >> bits) & BASE64_MAX; + psenc->ch[st->chlen++] = base64[i]; + } + } + + return (0); +} + +static int +_citrus_UTF7_wcrtomb_priv(_UTF7EncodingInfo * __restrict ei, + char * __restrict s, size_t n, wchar_t wchar, + _UTF7State * __restrict psenc, size_t * __restrict nresult) +{ + _UTF7StatePriv sv, *st; + uint32_t u32; + uint16_t u16[2]; + int err, len, i; + size_t nr; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(*nresult != NULL); + + u32 = (uint32_t)wchar; + if (u32 <= UTF16_MAX) { + u16[0] = (uint16_t)u32; + len = 1; + } else if (u32 <= UTF32_MAX) { + u32 -= SRG_BASE; + u16[0] = (u32 >> 10) + HISRG_MIN; + u16[1] = ((uint16_t)(u32 & UINT32_C(0x3ff))) + LOSRG_MIN; + len = 2; + } else { + *nresult = (size_t)-1; + return (EILSEQ); + } + + sv = *(st = &psenc->state); + nr = 0; + for (i = 0; i < len; i++) { + err = _utf16tomb(ei, u16[i], psenc); + switch (err) { + case 0: + if (st->chlen <= n) + break; + *st = sv; + err = (E2BIG); + case EILSEQ: + *nresult = (size_t)-1; + /*FALLTHROUGH*/ + default: + return (err); + } + n -= st->chlen; + memcpy(s, psenc->ch, st->chlen); + s += st->chlen; + nr += st->chlen; + st->chlen = 0; + } + *nresult = nr; + + return (0); +} + +static int +/* ARGSUSED */ +_citrus_UTF7_put_state_reset(_UTF7EncodingInfo * __restrict ei, + char * __restrict s, size_t n, _UTF7State * __restrict psenc, + size_t * __restrict nresult) +{ + _UTF7StatePriv *st; + int bits, pos; + + _DIAGASSERT(ei != NULL); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + _DIAGASSERT(nresult != NULL); + + st = &psenc->state; + if (st->chlen != 0 || st->bits > BASE64_BIT || st->surrogate) + return (EINVAL); + + if (st->mode) { + if (st->bits > 0) { + if (n-- < 1) + return (E2BIG); + bits = BASE64_BIT - st->bits; + pos = (st->cache << bits) & BASE64_MAX; + psenc->ch[st->chlen++] = base64[pos]; + psenc->ch[st->chlen++] = BASE64_OUT; + st->bits = st->cache = 0; + } + st->mode = 0; + } + if (st->bits != 0) + return (EINVAL); + if (n-- < 1) + return (E2BIG); + + _DIAGASSERT(n >= st->chlen); + *nresult = (size_t)st->chlen; + if (st->chlen > 0) { + memcpy(s, psenc->ch, st->chlen); + st->chlen = 0; + } + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF7_stdenc_wctocs(_UTF7EncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, wchar_t wc) +{ + /* ei seem to be unused */ + _DIAGASSERT(csid != NULL); + _DIAGASSERT(idx != NULL); + + *csid = 0; + *idx = (_index_t)wc; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF7_stdenc_cstowc(_UTF7EncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + /* ei seem to be unused */ + _DIAGASSERT(wc != NULL); + + if (csid != 0) + return (EILSEQ); + *wc = (wchar_t)idx; + + return (0); +} + +static void +/*ARGSUSED*/ +_citrus_UTF7_encoding_module_uninit(_UTF7EncodingInfo *ei) +{ + /* ei seems to be unused */ +} + + +static int +/*ARGSUSED*/ +_citrus_UTF7_encoding_module_init(_UTF7EncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + const char *s; + + _DIAGASSERT(ei != NULL); + /* var may be null */ + + memset(ei, 0, sizeof(*ei)); + +#define FILL(str, flag) \ +do { \ + for (s = str; *s != '\0'; s++) \ + ei->cell[*s & 0x7f] |= flag; \ +} while (/*CONSTCOND*/0) + + FILL(base64, (s - base64) + 1); + FILL(direct, EI_DIRECT); + FILL(option, EI_OPTION); + FILL(spaces, EI_SPACE); + + return (0); +} + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(UTF7); +_CITRUS_CTYPE_DEF_OPS(UTF7); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(UTF7); +_CITRUS_STDENC_DEF_OPS(UTF7); + +#include "citrus_stdenc_template.h" diff --git a/lib/libc/citrus/modules/citrus_utf7.h b/lib/libc/citrus/modules/citrus_utf7.h new file mode 100644 index 0000000000..cfb7ff21c0 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_utf7.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_utf7.h,v 1.1 2005/03/05 18:05:15 tnozaki Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_utf7.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2004,2005 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_UTF7_H_ +#define _CITRUS_UTF7_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(UTF7); +_CITRUS_STDENC_GETOPS_FUNC(UTF7); +__END_DECLS + +#endif diff --git a/lib/libc/citrus/modules/citrus_utf8.c b/lib/libc/citrus/modules/citrus_utf8.c new file mode 100644 index 0000000000..579c08fb31 --- /dev/null +++ b/lib/libc/citrus/modules/citrus_utf8.c @@ -0,0 +1,395 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_utf8.c,v 1.11 2004/12/21 11:25:43 yamt Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_utf8.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "citrus_namespace.h" +#include "citrus_types.h" +#include "citrus_module.h" +#include "citrus_ctype.h" +#include "citrus_stdenc.h" +#include "citrus_utf8.h" + + +/* ---------------------------------------------------------------------- + * private stuffs used by templates + */ + +static int _UTF8_count_array[256]; +static int const *_UTF8_count = NULL; + +static const u_int32_t _UTF8_range[] = { + 0, /*dummy*/ + 0x00000000, 0x00000080, 0x00000800, 0x00010000, + 0x00200000, 0x04000000, 0x80000000, +}; + +typedef struct { + char ch[6]; + int chlen; +} _UTF8State; + +typedef struct { +} _UTF8EncodingInfo; + +typedef struct { + _UTF8EncodingInfo ei; + struct { + /* for future multi-locale facility */ + _UTF8State s_mblen; + _UTF8State s_mbrlen; + _UTF8State s_mbrtowc; + _UTF8State s_mbtowc; + _UTF8State s_mbsrtowcs; + _UTF8State s_wcrtomb; + _UTF8State s_wcsrtombs; + _UTF8State s_wctomb; + } states; +} _UTF8CTypeInfo; + +#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) +#define _CEI_TO_STATE(_ei_, _func_) (_ei_)->states.s_##_func_ + +#define _FUNCNAME(m) _citrus_UTF8_##m +#define _ENCODING_INFO _UTF8EncodingInfo +#define _CTYPE_INFO _UTF8CTypeInfo +#define _ENCODING_STATE _UTF8State +#define _ENCODING_MB_CUR_MAX(_ei_) 6 +#define _ENCODING_IS_STATE_DEPENDENT 0 +#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 + + +static __inline void +_UTF8_init_count(void) +{ + int i; + if (!_UTF8_count) { + memset(_UTF8_count_array, 0, sizeof(_UTF8_count_array)); + for (i = 0; i <= 0x7f; i++) + _UTF8_count_array[i] = 1; + for (i = 0xc0; i <= 0xdf; i++) + _UTF8_count_array[i] = 2; + for (i = 0xe0; i <= 0xef; i++) + _UTF8_count_array[i] = 3; + for (i = 0xf0; i <= 0xf7; i++) + _UTF8_count_array[i] = 4; + for (i = 0xf8; i <= 0xfb; i++) + _UTF8_count_array[i] = 5; + for (i = 0xfc; i <= 0xfd; i++) + _UTF8_count_array[i] = 6; + _UTF8_count = _UTF8_count_array; + } +} + +static int +_UTF8_findlen(wchar_t v) +{ + int i; + u_int32_t c; + + c = (u_int32_t)v; /*XXX*/ + for (i = 1; i < sizeof(_UTF8_range) / sizeof(_UTF8_range[0]); i++) + if (c >= _UTF8_range[i] && c < _UTF8_range[i + 1]) + return i; + + return -1; /*out of range*/ +} + +static __inline void +/*ARGSUSED*/ +_citrus_UTF8_init_state(_UTF8EncodingInfo *ei, _UTF8State *s) +{ + memset(s, 0, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_UTF8_pack_state(_UTF8EncodingInfo *ei, void *pspriv, + const _UTF8State *s) +{ + memcpy(pspriv, (const void *)s, sizeof(*s)); +} + +static __inline void +/*ARGSUSED*/ +_citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei, _UTF8State *s, + const void *pspriv) +{ + memcpy((void *)s, pspriv, sizeof(*s)); +} + +static int +_citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, const char **s, + size_t n, _UTF8State *psenc, size_t *nresult) +{ + wchar_t wchar; + const char *s0; + int c; + int i; + int chlenbak; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + _DIAGASSERT(psenc != NULL); + + s0 = *s; + + if (s0 == NULL) { + _citrus_UTF8_init_state(ei, psenc); + *nresult = 0; /* state independent */ + return (0); + } + + chlenbak = psenc->chlen; + + /* make sure we have the first byte in the buffer */ + switch (psenc->chlen) { + case 0: + if (n < 1) { + goto restart; + } + psenc->ch[0] = *s0++; + psenc->chlen = 1; + n--; + break; + case 1: case 2: case 3: case 4: case 5: + break; + default: + /* illegal state */ + goto ilseq; + } + + c = _UTF8_count[psenc->ch[0] & 0xff]; + if (c == 0) + goto ilseq; + while (psenc->chlen < c) { + if (n < 1) { + goto restart; + } + psenc->ch[psenc->chlen] = *s0++; + psenc->chlen++; + n--; + } + + switch (c) { + case 1: + wchar = psenc->ch[0] & 0xff; + break; + case 2: case 3: case 4: case 5: case 6: + wchar = psenc->ch[0] & (0x7f >> c); + for (i = 1; i < c; i++) { + if ((psenc->ch[i] & 0xc0) != 0x80) + goto ilseq; + wchar <<= 6; + wchar |= (psenc->ch[i] & 0x3f); + } + + break; + } + + *s = s0; + + psenc->chlen = 0; + + if (pwc) + *pwc = wchar; + + if (!wchar) + *nresult = 0; + else + *nresult = c - chlenbak; + + return (0); + +ilseq: + psenc->chlen = 0; + *nresult = (size_t)-1; + return (EILSEQ); + +restart: + *s = s0; + *nresult = (size_t)-2; + return (0); +} + +static int +_citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei, char *s, size_t n, wchar_t wc, + _UTF8State *psenc, size_t *nresult) +{ + int cnt, i, ret; + wchar_t c; + + _DIAGASSERT(nresult != 0); + _DIAGASSERT(s != NULL); + + cnt = _UTF8_findlen(wc); + if (cnt <= 0 || cnt > 6) { + /* invalid UCS4 value */ + ret = EILSEQ; + goto err; + } + if (n < cnt) { + /* bound check failure */ + ret = E2BIG; + goto err; + } + + c = wc; + if (s) { + for (i = cnt - 1; i > 0; i--) { + s[i] = 0x80 | (c & 0x3f); + c >>= 6; + } + s[0] = c; + if (cnt == 1) + s[0] &= 0x7f; + else { + s[0] &= (0x7f >> cnt); + s[0] |= ((0xff00 >> cnt) & 0xff); + } + } + + *nresult = (size_t)cnt; + return 0; + +err: + *nresult = (size_t)-1; + return ret; +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF8_stdenc_wctocs(_UTF8EncodingInfo * __restrict ei, + _csid_t * __restrict csid, + _index_t * __restrict idx, + wchar_t wc) +{ + + _DIAGASSERT(csid != NULL && idx != NULL); + + *csid = 0; + *idx = (_citrus_index_t)wc; + + return (0); +} + +static __inline int +/*ARGSUSED*/ +_citrus_UTF8_stdenc_cstowc(_UTF8EncodingInfo * __restrict ei, + wchar_t * __restrict wc, + _csid_t csid, _index_t idx) +{ + + _DIAGASSERT(wc != NULL); + + if (csid != 0) + return (EILSEQ); + + *wc = (wchar_t)idx; + + return (0); +} + +static int +/*ARGSUSED*/ +_citrus_UTF8_encoding_module_init(_UTF8EncodingInfo * __restrict ei, + const void * __restrict var, size_t lenvar) +{ + _UTF8_init_count(); + + memset(ei, 0, sizeof(*ei)); + + return 0; +} + +static void +/*ARGSUSED*/ +_citrus_UTF8_encoding_module_uninit(_UTF8EncodingInfo *ei) +{ +} + + +/* ---------------------------------------------------------------------- + * public interface for ctype + */ + +_CITRUS_CTYPE_DECLS(UTF8); +_CITRUS_CTYPE_DEF_OPS(UTF8); + +#include "citrus_ctype_template.h" + +/* ---------------------------------------------------------------------- + * public interface for stdenc + */ + +_CITRUS_STDENC_DECLS(UTF8); +_CITRUS_STDENC_DEF_OPS(UTF8); + +#include "citrus_stdenc_template.h" diff --git a/lib/libc/citrus/modules/citrus_utf8.h b/lib/libc/citrus/modules/citrus_utf8.h new file mode 100644 index 0000000000..ee4fb7a90f --- /dev/null +++ b/lib/libc/citrus/modules/citrus_utf8.h @@ -0,0 +1,38 @@ +/* $NetBSD: src/lib/libc/citrus/modules/citrus_utf8.h,v 1.2 2003/06/25 09:51:49 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/citrus/modules/citrus_utf8.h,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2002 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CITRUS_UTF8_H_ +#define _CITRUS_UTF8_H_ + +__BEGIN_DECLS +_CITRUS_CTYPE_GETOPS_FUNC(UTF8); +_CITRUS_STDENC_GETOPS_FUNC(UTF8); +__END_DECLS + +#endif diff --git a/lib/libc/iconv/Makefile.inc b/lib/libc/iconv/Makefile.inc new file mode 100644 index 0000000000..ef24e84ea8 --- /dev/null +++ b/lib/libc/iconv/Makefile.inc @@ -0,0 +1,7 @@ +# $DragonFly: src/lib/libc/iconv/Makefile.inc,v 1.1 2005/03/11 23:33:53 joerg Exp $ + +.PATH: ${.CURDIR}/../libc/iconv + +SRCS+= iconv.c +MAN+= iconv.3 +MLINKS+=iconv.3 iconv_open.3 iconv.3 iconv_close.3 diff --git a/lib/libc/iconv/iconv.3 b/lib/libc/iconv/iconv.3 new file mode 100644 index 0000000000..482f021db3 --- /dev/null +++ b/lib/libc/iconv/iconv.3 @@ -0,0 +1,245 @@ +.\" $NetBSD: src/lib/libc/iconv/iconv.3,v 1.12 2004/08/02 13:38:21 tshiozak Exp $ +.\" $DragonFly: src/lib/libc/iconv/iconv.3,v 1.1 2005/03/11 23:33:53 joerg Exp $ +.\" +.\" Copyright (c)2003 Citrus Project, +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd August 1, 2004 +.Dt ICONV 3 +.Os +.\" ---------------------------------------------------------------------- +.Sh NAME +.Nm iconv_open , +.Nm iconv_close , +.Nm iconv +.Nd codeset conversion functions +.\" ---------------------------------------------------------------------- +.Sh LIBRARY +.Lb libc +.\" ---------------------------------------------------------------------- +.Sh SYNOPSIS +.In iconv.h +.Ft iconv_t +.Fn iconv_open "const char *dstname" "const char *srcname" +.Ft int +.Fn iconv_close "iconv_t cd" +.Ft size_t +.Fn iconv "iconv_t cd" "char ** restrict src" "size_t * restrict srcleft" "char ** restrict dst" "size_t * restrict dstleft" +.\" ---------------------------------------------------------------------- +.Sh DESCRIPTION +The +.Fn iconv_open +function opens a converter from the codeset +.Fa srcname +to the codeset +.Fa dstname +and returns its descriptor. +.Pp +The +.Fn iconv_close +function closes the specified converter +.Fa cd . +.Pp +The +.Fn iconv +function converts the string in the buffer +.Fa *src +of length +.Fa *srcleft +bytes and stores the converted string in the buffer +.Fa *dst +of size +.Fa *dstleft +bytes. +After calling +.Fn iconv , +the values pointed to by +.Fa src , +.Fa srcleft , +.Fa dst , +and +.Fa dstleft +are updated as follows: +.Bl -tag -width 01234567 +.It *src +Pointer to the byte just after the last character fetched. +.It *srcleft +Number of remaining bytes in the source buffer. +.It *dst +Pointer to the byte just after the last character stored. +.It *dstleft +Number of remainder bytes in the destination buffer. +.El +.Pp +If the string pointed to by +.Fa *src +contains a byte sequence which is not a valid character in the source +codeset, the conversion stops just after the last successful conversion. +If the output buffer is too small to store the converted +character, the conversion also stops in the same way. +In these cases, the values pointed to by +.Fa src , +.Fa srcleft , +.Fa dst , +and +.Fa dstleft +are updated to the state just after the last successful conversion. +.Pp +If the string pointed to by +.Fa *src +contains a character which is valid under the source codeset but +can not be converted to the destination codeset, +the character is replaced by an +.Dq invalid character +which depends on the destination codeset, e.g., +.Sq \&? , +and the conversion is continued. +.Fn iconv +returns the number of such +.Dq invalid conversions . +.Pp +There are two special cases of +.Fn iconv : +.Bl -tag -width 0123 +.It "src == NULL || *src == NULL" +.\" +If the source and/or destination codesets are stateful, +.Fn iconv +places these into their initial state. +.Pp +If both +.Fa dst +and +.Fa *dst +are +.No non- Ns Dv NULL , +.Fn iconv +stores the shift sequence for the destination switching to the initial state +in the buffer pointed to by +.Fa *dst . +The buffer size is specified by the value pointed to by +.Fa dstleft +as above. +.Fn iconv +will fail if the buffer is too small to store the shift sequence. +.Pp +On the other hand, +.Fa dst +or +.Fa *dst +may be +.Dv NULL . +In this case, the shift sequence for the destination switching +to the initial state is discarded. +.El +.\" ---------------------------------------------------------------------- +.Sh RETURN VALUES +Upon successful completion of +.Fn iconv_open , +it returns a conversion descriptor. +Otherwise, +.Fn iconv_open +returns (iconv_t)\-1 and sets errno to indicate the error. +.Pp +Upon successful completion of +.Fn iconv_close , +it returns 0. +Otherwise, +.Fn iconv_close +returns \-1 and sets errno to indicate the error. +.Pp +Upon successful completion of +.Fn iconv , +it returns the number of +.Dq invalid +conversions. +Otherwise, +.Fn iconv +returns (size_t)\-1 and sets errno to indicate the error. +.\" ---------------------------------------------------------------------- +.Sh ERRORS +The +.Fn iconv_open +function may cause an error in the following cases: +.Bl -tag -width Er +.It Bq Er ENOMEM +Memory is exhausted. +.It Bq Er EINVAL +There is no converter specified by +.Fa srcname +and +.Fa dstname . +.El +.Pp +The +.Fn iconv_close +function may cause an error in the following case: +.Bl -tag -width Er +.It Bq Er EBADF +The conversion descriptor specified by +.Fa cd +is invalid. +.El +.Pp +The +.Fn iconv +function may cause an error in the following cases: +.Bl -tag -width Er +.It Bq Er EBADF +The conversion descriptor specified by +.Fa cd +is invalid. +.It Bq Er EILSEQ +The string pointed to by +.Fa *src +contains a byte sequence which does not describe a valid character of +the source codeset. +.It Bq Er E2BIG +The output buffer pointed to by +.Fa *dst +is too small to store the result string. +.It Bq Er EINVAL +The string pointed to by +.Fa *src +terminates with an incomplete character or shift sequence. +.El +.\" ---------------------------------------------------------------------- +.Sh SEE ALSO +.Xr iconv 1 +.\" ---------------------------------------------------------------------- +.Sh STANDARDS +.Fn iconv_open , +.Fn iconv_close , +and +.Fn iconv +conform to +.St -p1003.1-2001 . +.\" ---------------------------------------------------------------------- +.Sh BUGS +If +.Fn iconv +is aborted due to the occurrence of some error, +the +.Dq invalid conversion +count mentioned above is unfortunately lost. diff --git a/lib/libc/iconv/iconv.c b/lib/libc/iconv/iconv.c new file mode 100644 index 0000000000..08697b7124 --- /dev/null +++ b/lib/libc/iconv/iconv.c @@ -0,0 +1,143 @@ +/* $NetBSD: src/lib/libc/iconv/iconv.c,v 1.4 2004/08/02 13:38:21 tshiozak Exp $ */ +/* $DragonFly: src/lib/libc/iconv/iconv.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */ + +/*- + * Copyright (c)2003 Citrus Project, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include + +#include +#include "../citrus/citrus_types.h" +#include "../citrus/citrus_module.h" +#include "../citrus/citrus_esdb.h" +#include "../citrus/citrus_hash.h" +#include "../citrus/citrus_iconv.h" + +#define ISBADF(_h_) (!(_h_) || (_h_) == (iconv_t)-1) + +iconv_t +_iconv_open(const char *out, const char *in) +{ + int ret; + struct _citrus_iconv *handle; + + ret = _citrus_iconv_open(&handle, _PATH_ICONV, in, out); + if (ret) { + errno = ret; + return ((iconv_t)-1); + } + + return ((iconv_t)(void *)handle); +} + +int +_iconv_close(iconv_t handle) +{ + if (ISBADF(handle)) { + errno = EBADF; + return (-1); + } + + _citrus_iconv_close((struct _citrus_iconv *)(void *)handle); + + return (0); +} + +size_t +_iconv(iconv_t handle, const char **in, size_t *szin, char **out, size_t *szout) +{ + int err; + size_t ret; + + if (ISBADF(handle)) { + errno = EBADF; + return ((size_t)-1); + } + + err = _citrus_iconv_convert( + (struct _citrus_iconv *)(void *)handle, in, szin, out, szout, + 0, &ret); + if (err) { + errno = err; + ret = (size_t)-1; + } + + return (ret); +} + +size_t +__iconv(iconv_t handle, const char **in, size_t *szin, char **out, + size_t *szout, u_int32_t flags, size_t *invalids) +{ + int err; + size_t ret; + + if (ISBADF(handle)) { + errno = EBADF; + return ((size_t)-1); + } + + err = _citrus_iconv_convert( + (struct _citrus_iconv *)(void *)handle, in, szin, out, szout, + flags, &ret); + if (invalids) + *invalids = ret; + if (err) { + errno = err; + ret = (size_t)-1; + } + + return (ret); +} + +int +__iconv_get_list(char ***rlist, size_t *rsz) +{ + int ret; + + ret = _citrus_esdb_get_list(rlist, rsz); + if (ret) { + errno = ret; + return -1; + } + + return 0; +} + +void +__iconv_free_list(char **list, size_t sz) +{ + _citrus_esdb_free_list(list, sz); +} + +__weak_reference(_iconv, iconv); +__weak_reference(_iconv_open, iconv_open); +__weak_reference(_iconv_close, iconv_close); -- 2.41.0