Index: head/include/iconv.h =================================================================== --- head/include/iconv.h (revision 252546) +++ head/include/iconv.h (revision 252547) @@ -1,138 +1,138 @@ /* $FreeBSD$ */ /* $NetBSD: iconv.h,v 1.6 2005/02/03 04:39:32 perry Exp $ */ /*- * Copyright (c) 2003 Citrus Project, * Copyright (c) 2009, 2010 Gabor Kovesdan * 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 #include #include #include #include #include #define iconv_open libiconv_open #define iconv_close libiconv_close -#define iconv(cd, in, insize, out, outsize) libiconv(cd, __DECONST(char **, in), insize, out, outsize) +#define iconv libiconv #define iconv_t libiconv_t struct __tag_iconv_t; typedef struct __tag_iconv_t *iconv_t; __BEGIN_DECLS iconv_t libiconv_open(const char *, const char *); -size_t libiconv(iconv_t, char ** __restrict, +size_t libiconv(iconv_t, const char ** __restrict, size_t * __restrict, char ** __restrict, size_t * __restrict); int libiconv_close(iconv_t); /* * non-portable interfaces for iconv */ int __iconv_get_list(char ***, size_t *, bool); void __iconv_free_list(char **, size_t); -size_t __iconv(iconv_t, char **, size_t *, char **, +size_t __iconv(iconv_t, const char **, size_t *, char **, size_t *, __uint32_t, size_t *); #define __ICONV_F_HIDE_INVALID 0x0001 /* * GNU interfaces for iconv */ #define iconv_open_into libiconv_open_into #define iconvctl libiconvctl #define iconvlist libiconvlist /* We have iconvctl() */ #define _LIBICONV_VERSION 0x0108 extern int _libiconv_version; typedef struct { void *spaceholder[64]; } iconv_allocation_t; int iconv_open_into(const char *, const char *, iconv_allocation_t *); void libiconv_set_relocation_prefix (const char *orig_prefix, const char *curr_prefix); /* * iconvctl() request macros */ #define ICONV_TRIVIALP 0 #define ICONV_GET_TRANSLITERATE 1 #define ICONV_SET_TRANSLITERATE 2 #define ICONV_GET_DISCARD_ILSEQ 3 #define ICONV_SET_DISCARD_ILSEQ 4 #define ICONV_SET_HOOKS 5 #define ICONV_SET_FALLBACKS 6 typedef void (*iconv_unicode_char_hook) (unsigned int mbr, void *data); typedef void (*iconv_wide_char_hook) (wchar_t wc, void *data); struct iconv_hooks { iconv_unicode_char_hook uc_hook; iconv_wide_char_hook wc_hook; void *data; }; /* * Fallbacks aren't supported but type definitions are provided for * source compatibility. */ typedef void (*iconv_unicode_mb_to_uc_fallback) (const char*, size_t, void (*write_replacement) (const unsigned int *, size_t, void*), void*, void*); typedef void (*iconv_unicode_uc_to_mb_fallback) (unsigned int, void (*write_replacement) (const char *, size_t, void*), void*, void*); typedef void (*iconv_wchar_mb_to_wc_fallback) (const char*, size_t, void (*write_replacement) (const wchar_t *, size_t, void*), void*, void*); typedef void (*iconv_wchar_wc_to_mb_fallback) (wchar_t, void (*write_replacement) (const char *, size_t, void*), void*, void*); struct iconv_fallbacks { iconv_unicode_mb_to_uc_fallback mb_to_uc_fallback; iconv_unicode_uc_to_mb_fallback uc_to_mb_fallback; iconv_wchar_mb_to_wc_fallback mb_to_wc_fallback; iconv_wchar_wc_to_mb_fallback wc_to_mb_fallback; void *data; }; void iconvlist(int (*do_one) (unsigned int, const char * const *, void *), void *); const char *iconv_canonicalize(const char *); int iconvctl(iconv_t, int, void *); __END_DECLS #endif /* !_ICONV_H_ */ Index: head/lib/libc/iconv/citrus_iconv.h =================================================================== --- head/lib/libc/iconv/citrus_iconv.h (revision 252546) +++ head/lib/libc/iconv/citrus_iconv.h (revision 252547) @@ -1,64 +1,64 @@ /* $FreeBSD$ */ /* $NetBSD: citrus_iconv.h,v 1.5 2008/02/09 14:56:20 junyoung 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); void _citrus_iconv_close(struct _citrus_iconv *); const char *_citrus_iconv_canonicalize(const char *); __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, - char * __restrict * __restrict in, size_t * __restrict inbytes, + const char * __restrict * __restrict in, size_t * __restrict inbytes, char * __restrict * __restrict out, size_t * __restrict outbytes, uint32_t flags, size_t * __restrict nresults) { return (*cv->cv_shared->ci_ops->io_convert)(cv, in, inbytes, out, outbytes, flags, nresults); } #endif Index: head/lib/libc/iconv/citrus_iconv_local.h =================================================================== --- head/lib/libc/iconv/citrus_iconv_local.h (revision 252546) +++ head/lib/libc/iconv/citrus_iconv_local.h (revision 252547) @@ -1,108 +1,108 @@ /* $FreeBSD$ */ /* $NetBSD: citrus_iconv_local.h,v 1.3 2008/02/09 14:56:20 junyoung 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_ #include #define _CITRUS_ICONV_GETOPS_FUNC_BASE(_n_) \ int _n_(struct _citrus_iconv_ops *) #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); \ static void _citrus_##_m_##_iconv_uninit_shared \ (struct _citrus_iconv_shared *); \ static int _citrus_##_m_##_iconv_convert \ (struct _citrus_iconv * __restrict, \ - char * __restrict * __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_) \ extern struct _citrus_iconv_ops _citrus_##_m_##_iconv_ops; \ struct _citrus_iconv_ops _citrus_##_m_##_iconv_ops = { \ /* 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); typedef void (*_citrus_iconv_uninit_shared_t) (struct _citrus_iconv_shared *); typedef int (*_citrus_iconv_convert_t) (struct _citrus_iconv * __restrict, - char *__restrict* __restrict, size_t * __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 { _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; }; struct _citrus_iconv_shared { struct _citrus_iconv_ops *ci_ops; void *ci_closure; _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; bool ci_discard_ilseq; struct iconv_hooks *ci_hooks; }; struct _citrus_iconv { struct _citrus_iconv_shared *cv_shared; void *cv_closure; }; #endif Index: head/lib/libc/iconv/iconv.c =================================================================== --- head/lib/libc/iconv/iconv.c (revision 252546) +++ head/lib/libc/iconv/iconv.c (revision 252547) @@ -1,323 +1,323 @@ /* $FreeBSD$ */ /* $NetBSD: iconv.c,v 1.11 2009/03/03 16:22:33 explorer Exp $ */ /*- * Copyright (c) 2003 Citrus Project, * Copyright (c) 2009, 2010 Gabor Kovesdan , * 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_types.h" #include "citrus_module.h" #include "citrus_esdb.h" #include "citrus_hash.h" #include "citrus_iconv.h" __weak_reference(libiconv, iconv); __weak_reference(libiconv_open, iconv_open); __weak_reference(libiconv_open_into, iconv_open_into); __weak_reference(libiconv_close, iconv_close); __weak_reference(libiconvlist, iconvlist); __weak_reference(libiconvctl, iconvctl); __weak_reference(libiconv_set_relocation_prefix, iconv_set_relocation_prefix); #define ISBADF(_h_) (!(_h_) || (_h_) == (iconv_t)-1) int _libiconv_version = _LIBICONV_VERSION; iconv_t _iconv_open(const char *out, const char *in, struct _citrus_iconv *prealloc); iconv_t _iconv_open(const char *out, const char *in, struct _citrus_iconv *handle) { const char *out_slashes; char *out_noslashes; int ret; /* * Remove anything following a //, as these are options (like * //ignore, //translate, etc) and we just don't handle them. * This is for compatibility with software that uses these * blindly. */ out_slashes = strstr(out, "//"); if (out_slashes != NULL) { out_noslashes = strndup(out, out_slashes - out); if (out_noslashes == NULL) { errno = ENOMEM; return ((iconv_t)-1); } ret = _citrus_iconv_open(&handle, in, out_noslashes); free(out_noslashes); } else { ret = _citrus_iconv_open(&handle, in, out); } if (ret) { errno = ret == ENOENT ? EINVAL : ret; return ((iconv_t)-1); } handle->cv_shared->ci_discard_ilseq = strcasestr(out, "//IGNORE"); handle->cv_shared->ci_hooks = NULL; return ((iconv_t)(void *)handle); } iconv_t libiconv_open(const char *out, const char *in) { return (_iconv_open(out, in, NULL)); } int libiconv_open_into(const char *out, const char *in, iconv_allocation_t *ptr) { struct _citrus_iconv *handle; handle = (struct _citrus_iconv *)ptr; return ((_iconv_open(out, in, handle) == (iconv_t)-1) ? -1 : 0); } int libiconv_close(iconv_t handle) { if (ISBADF(handle)) { errno = EBADF; return (-1); } _citrus_iconv_close((struct _citrus_iconv *)(void *)handle); return (0); } size_t -libiconv(iconv_t handle, char **in, size_t *szin, char **out, size_t *szout) +libiconv(iconv_t handle, const char **in, size_t *szin, char **out, size_t *szout) { size_t ret; int err; 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, char **in, size_t *szin, char **out, +__iconv(iconv_t handle, const char **in, size_t *szin, char **out, size_t *szout, uint32_t flags, size_t *invalids) { size_t ret; int err; 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, bool sorted) { int ret; ret = _citrus_esdb_get_list(rlist, rsz, sorted); if (ret) { errno = ret; return (-1); } return (0); } void __iconv_free_list(char **list, size_t sz) { _citrus_esdb_free_list(list, sz); } /* * GNU-compatibile non-standard interfaces. */ static int qsort_helper(const void *first, const void *second) { const char * const *s1; const char * const *s2; s1 = first; s2 = second; return (strcmp(*s1, *s2)); } void libiconvlist(int (*do_one) (unsigned int, const char * const *, void *), void *data) { char **list, **names; const char * const *np; char *curitem, *curkey, *slashpos; size_t sz; unsigned int i, j; i = 0; if (__iconv_get_list(&list, &sz, true)) list = NULL; qsort((void *)list, sz, sizeof(char *), qsort_helper); while (i < sz) { j = 0; slashpos = strchr(list[i], '/'); curkey = (char *)malloc(slashpos - list[i] + 2); names = (char **)malloc(sz * sizeof(char *)); if ((curkey == NULL) || (names == NULL)) { __iconv_free_list(list, sz); return; } strlcpy(curkey, list[i], slashpos - list[i] + 1); names[j++] = strdup(curkey); for (; (i < sz) && (memcmp(curkey, list[i], strlen(curkey)) == 0); i++) { slashpos = strchr(list[i], '/'); curitem = (char *)malloc(strlen(slashpos) + 1); if (curitem == NULL) { __iconv_free_list(list, sz); return; } strlcpy(curitem, &slashpos[1], strlen(slashpos) + 1); if (strcmp(curkey, curitem) == 0) { continue; } names[j++] = strdup(curitem); } np = (const char * const *)names; do_one(j, np, data); free(names); } __iconv_free_list(list, sz); } __inline const char *iconv_canonicalize(const char *name) { return (_citrus_iconv_canonicalize(name)); } int libiconvctl(iconv_t cd, int request, void *argument) { struct _citrus_iconv *cv; struct iconv_hooks *hooks; const char *convname; char src[PATH_MAX], *dst; int *i; cv = (struct _citrus_iconv *)(void *)cd; hooks = (struct iconv_hooks *)argument; i = (int *)argument; if (ISBADF(cd)) { errno = EBADF; return (-1); } switch (request) { case ICONV_TRIVIALP: convname = cv->cv_shared->ci_convname; dst = strchr(convname, '/'); strlcpy(src, convname, dst - convname + 1); dst++; if ((convname == NULL) || (src == NULL) || (dst == NULL)) return (-1); *i = strcmp(src, dst) == 0 ? 1 : 0; return (0); case ICONV_GET_TRANSLITERATE: *i = 1; return (0); case ICONV_SET_TRANSLITERATE: return ((*i == 1) ? 0 : -1); case ICONV_GET_DISCARD_ILSEQ: *i = cv->cv_shared->ci_discard_ilseq ? 1 : 0; return (0); case ICONV_SET_DISCARD_ILSEQ: cv->cv_shared->ci_discard_ilseq = *i; return (0); case ICONV_SET_HOOKS: cv->cv_shared->ci_hooks = hooks; return (0); case ICONV_SET_FALLBACKS: errno = EOPNOTSUPP; return (-1); default: errno = EINVAL; return (-1); } } void libiconv_set_relocation_prefix(const char *orig_prefix __unused, const char *curr_prefix __unused) { } Index: head/lib/libc/locale/cXXrtomb_iconv.h =================================================================== --- head/lib/libc/locale/cXXrtomb_iconv.h (revision 252546) +++ head/lib/libc/locale/cXXrtomb_iconv.h (revision 252547) @@ -1,115 +1,116 @@ /*- * Copyright (c) 2013 Ed Schouten * 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include #include "../iconv/citrus_hash.h" #include "../iconv/citrus_module.h" #include "../iconv/citrus_iconv.h" #include "xlocale_private.h" typedef struct { bool initialized; struct _citrus_iconv iconv; union { charXX_t widechar[SRCBUF_LEN]; char bytes[sizeof(charXX_t) * SRCBUF_LEN]; } srcbuf; size_t srcbuf_len; } _ConversionState; _Static_assert(sizeof(_ConversionState) <= sizeof(mbstate_t), "Size of _ConversionState must not exceed mbstate_t's size."); size_t cXXrtomb_l(char * __restrict s, charXX_t c, mbstate_t * __restrict ps, locale_t locale) { _ConversionState *cs; struct _citrus_iconv *handle; - char *src, *dst; + const char *src; + char *dst; size_t srcleft, dstleft, invlen; int err; FIX_LOCALE(locale); if (ps == NULL) ps = &locale->cXXrtomb; cs = (_ConversionState *)ps; handle = &cs->iconv; /* Reinitialize mbstate_t. */ if (s == NULL || !cs->initialized) { if (_citrus_iconv_open(&handle, UTF_XX_INTERNAL, nl_langinfo_l(CODESET, locale)) != 0) { cs->initialized = false; errno = EINVAL; return (-1); } handle->cv_shared->ci_discard_ilseq = true; handle->cv_shared->ci_hooks = NULL; cs->srcbuf_len = 0; cs->initialized = true; if (s == NULL) return (1); } assert(cs->srcbuf_len < sizeof(cs->srcbuf.widechar) / sizeof(charXX_t)); cs->srcbuf.widechar[cs->srcbuf_len++] = c; /* Perform conversion. */ src = cs->srcbuf.bytes; srcleft = cs->srcbuf_len * sizeof(charXX_t); dst = s; dstleft = MB_CUR_MAX_L(locale); err = _citrus_iconv_convert(handle, &src, &srcleft, &dst, &dstleft, 0, &invlen); /* Character is part of a surrogate pair. We need more input. */ if (err == EINVAL) return (0); cs->srcbuf_len = 0; /* Illegal sequence. */ if (dst == s) { errno = EILSEQ; return ((size_t)-1); } return (dst - s); } size_t cXXrtomb(char * __restrict s, charXX_t c, mbstate_t * __restrict ps) { return (cXXrtomb_l(s, c, ps, __get_locale())); } Index: head/lib/libc/locale/mbrtocXX_iconv.h =================================================================== --- head/lib/libc/locale/mbrtocXX_iconv.h (revision 252546) +++ head/lib/libc/locale/mbrtocXX_iconv.h (revision 252547) @@ -1,158 +1,159 @@ /*- * Copyright (c) 2013 Ed Schouten * 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include "../iconv/citrus_hash.h" #include "../iconv/citrus_module.h" #include "../iconv/citrus_iconv.h" #include "xlocale_private.h" typedef struct { bool initialized; struct _citrus_iconv iconv; char srcbuf[MB_LEN_MAX]; size_t srcbuf_len; union { charXX_t widechar[DSTBUF_LEN]; char bytes[sizeof(charXX_t) * DSTBUF_LEN]; } dstbuf; size_t dstbuf_len; } _ConversionState; _Static_assert(sizeof(_ConversionState) <= sizeof(mbstate_t), "Size of _ConversionState must not exceed mbstate_t's size."); size_t mbrtocXX_l(charXX_t * __restrict pc, const char * __restrict s, size_t n, mbstate_t * __restrict ps, locale_t locale) { _ConversionState *cs; struct _citrus_iconv *handle; size_t i, retval; charXX_t retchar; FIX_LOCALE(locale); if (ps == NULL) ps = &locale->mbrtocXX; cs = (_ConversionState *)ps; handle = &cs->iconv; /* Reinitialize mbstate_t. */ if (s == NULL || !cs->initialized) { if (_citrus_iconv_open(&handle, nl_langinfo_l(CODESET, locale), UTF_XX_INTERNAL) != 0) { cs->initialized = false; errno = EINVAL; return (-1); } handle->cv_shared->ci_discard_ilseq = true; handle->cv_shared->ci_hooks = NULL; cs->srcbuf_len = cs->dstbuf_len = 0; cs->initialized = true; if (s == NULL) return (0); } /* See if we still have characters left from the previous invocation. */ if (cs->dstbuf_len > 0) { retval = (size_t)-3; goto return_char; } /* Fill up the read buffer as far as possible. */ if (n > sizeof(cs->srcbuf) - cs->srcbuf_len) n = sizeof(cs->srcbuf) - cs->srcbuf_len; memcpy(cs->srcbuf + cs->srcbuf_len, s, n); /* Convert as few characters to the dst buffer as possible. */ for (i = 0; ; i++) { - char *src, *dst; + const char *src; + char *dst; size_t srcleft, dstleft, invlen; int err; src = cs->srcbuf; srcleft = cs->srcbuf_len + n; dst = cs->dstbuf.bytes; dstleft = i * sizeof(charXX_t); assert(srcleft <= sizeof(cs->srcbuf) && dstleft <= sizeof(cs->dstbuf.bytes)); err = _citrus_iconv_convert(handle, &src, &srcleft, &dst, &dstleft, 0, &invlen); cs->dstbuf_len = (dst - cs->dstbuf.bytes) / sizeof(charXX_t); /* Got new character(s). Return the first. */ if (cs->dstbuf_len > 0) { assert(src - cs->srcbuf > cs->srcbuf_len); retval = src - cs->srcbuf - cs->srcbuf_len; cs->srcbuf_len = 0; goto return_char; } /* Increase dst buffer size, to obtain the surrogate pair. */ if (err == E2BIG) continue; /* Illegal sequence. */ if (invlen > 0) { cs->srcbuf_len = 0; errno = EILSEQ; return ((size_t)-1); } /* Save unprocessed remainder for the next invocation. */ memmove(cs->srcbuf, src, srcleft); cs->srcbuf_len = srcleft; return ((size_t)-2); } return_char: retchar = cs->dstbuf.widechar[0]; memmove(&cs->dstbuf.widechar[0], &cs->dstbuf.widechar[1], --cs->dstbuf_len * sizeof(charXX_t)); if (pc != NULL) *pc = retchar; if (retchar == 0) return (0); return (retval); } size_t mbrtocXX(charXX_t * __restrict pc, const char * __restrict s, size_t n, mbstate_t * __restrict ps) { return (mbrtocXX_l(pc, s, n, ps, __get_locale())); }