Deleted Added
full compact
cXXrtomb_iconv.h (252547) cXXrtomb_iconv.h (281550)
1/*-
2 * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/lib/libc/locale/cXXrtomb_iconv.h 252547 2013-07-03 07:03:19Z peter $");
28__FBSDID("$FreeBSD: head/lib/libc/locale/cXXrtomb_iconv.h 281550 2015-04-15 09:09:20Z tijl $");
29
30#include <sys/queue.h>
31
32#include <assert.h>
33#include <errno.h>
34#include <langinfo.h>
35#include <uchar.h>
36
37#include "../iconv/citrus_hash.h"
38#include "../iconv/citrus_module.h"
39#include "../iconv/citrus_iconv.h"
40#include "xlocale_private.h"
41
42typedef struct {
43 bool initialized;
44 struct _citrus_iconv iconv;
45 union {
46 charXX_t widechar[SRCBUF_LEN];
47 char bytes[sizeof(charXX_t) * SRCBUF_LEN];
48 } srcbuf;
49 size_t srcbuf_len;
50} _ConversionState;
51_Static_assert(sizeof(_ConversionState) <= sizeof(mbstate_t),
52 "Size of _ConversionState must not exceed mbstate_t's size.");
53
54size_t
55cXXrtomb_l(char * __restrict s, charXX_t c, mbstate_t * __restrict ps,
56 locale_t locale)
57{
58 _ConversionState *cs;
59 struct _citrus_iconv *handle;
29
30#include <sys/queue.h>
31
32#include <assert.h>
33#include <errno.h>
34#include <langinfo.h>
35#include <uchar.h>
36
37#include "../iconv/citrus_hash.h"
38#include "../iconv/citrus_module.h"
39#include "../iconv/citrus_iconv.h"
40#include "xlocale_private.h"
41
42typedef struct {
43 bool initialized;
44 struct _citrus_iconv iconv;
45 union {
46 charXX_t widechar[SRCBUF_LEN];
47 char bytes[sizeof(charXX_t) * SRCBUF_LEN];
48 } srcbuf;
49 size_t srcbuf_len;
50} _ConversionState;
51_Static_assert(sizeof(_ConversionState) <= sizeof(mbstate_t),
52 "Size of _ConversionState must not exceed mbstate_t's size.");
53
54size_t
55cXXrtomb_l(char * __restrict s, charXX_t c, mbstate_t * __restrict ps,
56 locale_t locale)
57{
58 _ConversionState *cs;
59 struct _citrus_iconv *handle;
60 const char *src;
61 char *dst;
60 char *src, *dst;
62 size_t srcleft, dstleft, invlen;
63 int err;
64
65 FIX_LOCALE(locale);
66 if (ps == NULL)
67 ps = &locale->cXXrtomb;
68 cs = (_ConversionState *)ps;
69 handle = &cs->iconv;
70
71 /* Reinitialize mbstate_t. */
72 if (s == NULL || !cs->initialized) {
73 if (_citrus_iconv_open(&handle, UTF_XX_INTERNAL,
74 nl_langinfo_l(CODESET, locale)) != 0) {
75 cs->initialized = false;
76 errno = EINVAL;
77 return (-1);
78 }
79 handle->cv_shared->ci_discard_ilseq = true;
80 handle->cv_shared->ci_hooks = NULL;
81 cs->srcbuf_len = 0;
82 cs->initialized = true;
83 if (s == NULL)
84 return (1);
85 }
86
87 assert(cs->srcbuf_len < sizeof(cs->srcbuf.widechar) / sizeof(charXX_t));
88 cs->srcbuf.widechar[cs->srcbuf_len++] = c;
89
90 /* Perform conversion. */
91 src = cs->srcbuf.bytes;
92 srcleft = cs->srcbuf_len * sizeof(charXX_t);
93 dst = s;
94 dstleft = MB_CUR_MAX_L(locale);
95 err = _citrus_iconv_convert(handle, &src, &srcleft, &dst, &dstleft,
96 0, &invlen);
97
98 /* Character is part of a surrogate pair. We need more input. */
99 if (err == EINVAL)
100 return (0);
101 cs->srcbuf_len = 0;
102
103 /* Illegal sequence. */
104 if (dst == s) {
105 errno = EILSEQ;
106 return ((size_t)-1);
107 }
108 return (dst - s);
109}
110
111size_t
112cXXrtomb(char * __restrict s, charXX_t c, mbstate_t * __restrict ps)
113{
114
115 return (cXXrtomb_l(s, c, ps, __get_locale()));
116}
61 size_t srcleft, dstleft, invlen;
62 int err;
63
64 FIX_LOCALE(locale);
65 if (ps == NULL)
66 ps = &locale->cXXrtomb;
67 cs = (_ConversionState *)ps;
68 handle = &cs->iconv;
69
70 /* Reinitialize mbstate_t. */
71 if (s == NULL || !cs->initialized) {
72 if (_citrus_iconv_open(&handle, UTF_XX_INTERNAL,
73 nl_langinfo_l(CODESET, locale)) != 0) {
74 cs->initialized = false;
75 errno = EINVAL;
76 return (-1);
77 }
78 handle->cv_shared->ci_discard_ilseq = true;
79 handle->cv_shared->ci_hooks = NULL;
80 cs->srcbuf_len = 0;
81 cs->initialized = true;
82 if (s == NULL)
83 return (1);
84 }
85
86 assert(cs->srcbuf_len < sizeof(cs->srcbuf.widechar) / sizeof(charXX_t));
87 cs->srcbuf.widechar[cs->srcbuf_len++] = c;
88
89 /* Perform conversion. */
90 src = cs->srcbuf.bytes;
91 srcleft = cs->srcbuf_len * sizeof(charXX_t);
92 dst = s;
93 dstleft = MB_CUR_MAX_L(locale);
94 err = _citrus_iconv_convert(handle, &src, &srcleft, &dst, &dstleft,
95 0, &invlen);
96
97 /* Character is part of a surrogate pair. We need more input. */
98 if (err == EINVAL)
99 return (0);
100 cs->srcbuf_len = 0;
101
102 /* Illegal sequence. */
103 if (dst == s) {
104 errno = EILSEQ;
105 return ((size_t)-1);
106 }
107 return (dst - s);
108}
109
110size_t
111cXXrtomb(char * __restrict s, charXX_t c, mbstate_t * __restrict ps)
112{
113
114 return (cXXrtomb_l(s, c, ps, __get_locale()));
115}